Debian Codegraph @

doc/classes/ConfigFile.xml

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <class name="ConfigFile" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3 <brief_description>
4 Helper class to handle INI-style files.
5 </brief_description>
6 <description>
7 This helper class can be used to store [Variant] values on the filesystem using INI-style formatting. The stored values are identified by a section and a key:
8 [codeblock lang=text]
9 [section]
10 some_key=42
11 string_example="Hello World3D!"
12 a_vector=Vector3(1, 0, 2)
13 [/codeblock]
14 The stored data can be saved to or parsed from a file, though ConfigFile objects can also be used directly without accessing the filesystem.
15 The following example shows how to create a simple [ConfigFile] and save it on disc:
16 [codeblocks]
17 [gdscript]
18 # Create new ConfigFile object.
19 var config = ConfigFile.new()
20
21 # Store some values.
22 config.set_value("Player1", "player_name", "Steve")
23 config.set_value("Player1", "best_score", 10)
24 config.set_value("Player2", "player_name", "V3geta")
25 config.set_value("Player2", "best_score", 9001)
26
27 # Save it to a file (overwrite if already exists).
28 config.save("user://scores.cfg")
29 [/gdscript]
30 [csharp]
31 // Create new ConfigFile object.
32 var config = new ConfigFile();
33
34 // Store some values.
35 config.SetValue("Player1", "player_name", "Steve");
36 config.SetValue("Player1", "best_score", 10);
37 config.SetValue("Player2", "player_name", "V3geta");
38 config.SetValue("Player2", "best_score", 9001);
39
40 // Save it to a file (overwrite if already exists).
41 config.Save("user://scores.cfg");
42 [/csharp]
43 [/codeblocks]
44 This example shows how the above file could be loaded:
45 [codeblocks]
46 [gdscript]
47 var score_data = {}
48 var config = ConfigFile.new()
49
50 # Load data from a file.
51 var err = config.load("user://scores.cfg")
52
53 # If the file didn't load, ignore it.
54 if err != OK:
55 return
56
57 # Iterate over all sections.
58 for player in config.get_sections():
59 # Fetch the data for each section.
60 var player_name = config.get_value(player, "player_name")
61 var player_score = config.get_value(player, "best_score")
62 score_data[player_name] = player_score
63 [/gdscript]
64 [csharp]
65 var score_data = new Godot.Collections.Dictionary();
66 var config = new ConfigFile();
67
68 // Load data from a file.
69 Error err = config.Load("user://scores.cfg");
70
71 // If the file didn't load, ignore it.
72 if (err != Error.Ok)
73 {
74 return;
75 }
76
77 // Iterate over all sections.
78 foreach (String player in config.GetSections())
79 {
80 // Fetch the data for each section.
81 var player_name = (String)config.GetValue(player, "player_name");
82 var player_score = (int)config.GetValue(player, "best_score");
83 score_data[player_name] = player_score;
84 }
85 [/csharp]
86 [/codeblocks]
87 Any operation that mutates the ConfigFile such as [method set_value], [method clear], or [method erase_section], only changes what is loaded in memory. If you want to write the change to a file, you have to save the changes with [method save], [method save_encrypted], or [method save_encrypted_pass].
88 Keep in mind that section and property names can't contain spaces. Anything after a space will be ignored on save and on load.
89 ConfigFiles can also contain manually written comment lines starting with a semicolon ([code];[/code]). Those lines will be ignored when parsing the file. Note that comments will be lost when saving the ConfigFile. This can still be useful for dedicated server configuration files, which are typically never overwritten without explicit user action.
90 [b]Note:[/b] The file extension given to a ConfigFile does not have any impact on its formatting or behavior. By convention, the [code].cfg[/code] extension is used here, but any other extension such as [code].ini[/code] is also valid. Since neither [code].cfg[/code] nor [code].ini[/code] are standardized, Godot's ConfigFile formatting may differ from files written by other programs.
91 </description>
92 <tutorials>
93 </tutorials>
94 <methods>
95 <method name="clear">
96 <return type="void" />
97 <description>
98 Removes the entire contents of the config.
99 </description>
100 </method>
101 <method name="encode_to_text" qualifiers="const">
102 <return type="String" />
103 <description>
104 Obtain the text version of this config file (the same text that would be written to a file).
105 </description>
106 </method>
107 <method name="erase_section">
108 <return type="void" />
109 <param index="0" name="section" type="String" />
110 <description>
111 Deletes the specified section along with all the key-value pairs inside. Raises an error if the section does not exist.
112 </description>
113 </method>
114 <method name="erase_section_key">
115 <return type="void" />
116 <param index="0" name="section" type="String" />
117 <param index="1" name="key" type="String" />
118 <description>
119 Deletes the specified key in a section. Raises an error if either the section or the key do not exist.
120 </description>
121 </method>
122 <method name="get_section_keys" qualifiers="const">
123 <return type="PackedStringArray" />
124 <param index="0" name="section" type="String" />
125 <description>
126 Returns an array of all defined key identifiers in the specified section. Raises an error and returns an empty array if the section does not exist.
127 </description>
128 </method>
129 <method name="get_sections" qualifiers="const">
130 <return type="PackedStringArray" />
131 <description>
132 Returns an array of all defined section identifiers.
133 </description>
134 </method>
135 <method name="get_value" qualifiers="const">
136 <return type="Variant" />
137 <param index="0" name="section" type="String" />
138 <param index="1" name="key" type="String" />
139 <param index="2" name="default" type="Variant" default="null" />
140 <description>
141 Returns the current value for the specified section and key. If either the section or the key do not exist, the method returns the fallback [param default] value. If [param default] is not specified or set to [code]null[/code], an error is also raised.
142 </description>
143 </method>
144 <method name="has_section" qualifiers="const">
145 <return type="bool" />
146 <param index="0" name="section" type="String" />
147 <description>
148 Returns [code]true[/code] if the specified section exists.
149 </description>
150 </method>
151 <method name="has_section_key" qualifiers="const">
152 <return type="bool" />
153 <param index="0" name="section" type="String" />
154 <param index="1" name="key" type="String" />
155 <description>
156 Returns [code]true[/code] if the specified section-key pair exists.
157 </description>
158 </method>
159 <method name="load">
160 <return type="int" enum="Error" />
161 <returns_error number="0"/>
162 <returns_error number="12"/>
163 <param index="0" name="path" type="String" />
164 <description>
165 Loads the config file specified as a parameter. The file's contents are parsed and loaded in the [ConfigFile] object which the method was called on.
166 Returns [constant OK] on success, or one of the other [enum Error] values if the operation failed.
167 </description>
168 </method>
169 <method name="load_encrypted">
170 <return type="int" enum="Error" />
171 <param index="0" name="path" type="String" />
172 <param index="1" name="key" type="PackedByteArray" />
173 <description>
174 Loads the encrypted config file specified as a parameter, using the provided [param key] to decrypt it. The file's contents are parsed and loaded in the [ConfigFile] object which the method was called on.
175 Returns [constant OK] on success, or one of the other [enum Error] values if the operation failed.
176 </description>
177 </method>
178 <method name="load_encrypted_pass">
179 <return type="int" enum="Error" />
180 <param index="0" name="path" type="String" />
181 <param index="1" name="password" type="String" />
182 <description>
183 Loads the encrypted config file specified as a parameter, using the provided [param password] to decrypt it. The file's contents are parsed and loaded in the [ConfigFile] object which the method was called on.
184 Returns [constant OK] on success, or one of the other [enum Error] values if the operation failed.
185 </description>
186 </method>
187 <method name="parse">
188 <return type="int" enum="Error" />
189 <param index="0" name="data" type="String" />
190 <description>
191 Parses the passed string as the contents of a config file. The string is parsed and loaded in the ConfigFile object which the method was called on.
192 Returns [constant OK] on success, or one of the other [enum Error] values if the operation failed.
193 </description>
194 </method>
195 <method name="save">
196 <return type="int" enum="Error" />
197 <param index="0" name="path" type="String" />
198 <description>
199 Saves the contents of the [ConfigFile] object to the file specified as a parameter. The output file uses an INI-style structure.
200 Returns [constant OK] on success, or one of the other [enum Error] values if the operation failed.
201 </description>
202 </method>
203 <method name="save_encrypted">
204 <return type="int" enum="Error" />
205 <param index="0" name="path" type="String" />
206 <param index="1" name="key" type="PackedByteArray" />
207 <description>
208 Saves the contents of the [ConfigFile] object to the AES-256 encrypted file specified as a parameter, using the provided [param key] to encrypt it. The output file uses an INI-style structure.
209 Returns [constant OK] on success, or one of the other [enum Error] values if the operation failed.
210 </description>
211 </method>
212 <method name="save_encrypted_pass">
213 <return type="int" enum="Error" />
214 <param index="0" name="path" type="String" />
215 <param index="1" name="password" type="String" />
216 <description>
217 Saves the contents of the [ConfigFile] object to the AES-256 encrypted file specified as a parameter, using the provided [param password] to encrypt it. The output file uses an INI-style structure.
218 Returns [constant OK] on success, or one of the other [enum Error] values if the operation failed.
219 </description>
220 </method>
221 <method name="set_value">
222 <return type="void" />
223 <param index="0" name="section" type="String" />
224 <param index="1" name="key" type="String" />
225 <param index="2" name="value" type="Variant" />
226 <description>
227 Assigns a value to the specified key of the specified section. If either the section or the key do not exist, they are created. Passing a [code]null[/code] value deletes the specified key if it exists, and deletes the section if it ends up empty once the key has been removed.
228 </description>
229 </method>
230 </methods>
231 </class>