class_configfile.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/ConfigFile.xml.
  6. .. _class_ConfigFile:
  7. ConfigFile
  8. ==========
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. Helper class to handle INI-style files.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. This helper class can be used to store :ref:`Variant<class_Variant>` values on the filesystem using INI-style formatting. The stored values are identified by a section and a key:
  15. .. code:: text
  16. [section]
  17. some_key=42
  18. string_example="Hello World3D!"
  19. a_vector=Vector3(1, 0, 2)
  20. The stored data can be saved to or parsed from a file, though ConfigFile objects can also be used directly without accessing the filesystem.
  21. The following example shows how to create a simple **ConfigFile** and save it on disc:
  22. .. tabs::
  23. .. code-tab:: gdscript
  24. # Create new ConfigFile object.
  25. var config = ConfigFile.new()
  26. # Store some values.
  27. config.set_value("Player1", "player_name", "Steve")
  28. config.set_value("Player1", "best_score", 10)
  29. config.set_value("Player2", "player_name", "V3geta")
  30. config.set_value("Player2", "best_score", 9001)
  31. # Save it to a file (overwrite if already exists).
  32. config.save("user://scores.cfg")
  33. .. code-tab:: csharp
  34. // Create new ConfigFile object.
  35. var config = new ConfigFile();
  36. // Store some values.
  37. config.SetValue("Player1", "player_name", "Steve");
  38. config.SetValue("Player1", "best_score", 10);
  39. config.SetValue("Player2", "player_name", "V3geta");
  40. config.SetValue("Player2", "best_score", 9001);
  41. // Save it to a file (overwrite if already exists).
  42. config.Save("user://scores.cfg");
  43. This example shows how the above file could be loaded:
  44. .. tabs::
  45. .. code-tab:: gdscript
  46. var score_data = {}
  47. var config = ConfigFile.new()
  48. # Load data from a file.
  49. var err = config.load("user://scores.cfg")
  50. # If the file didn't load, ignore it.
  51. if err != OK:
  52. return
  53. # Iterate over all sections.
  54. for player in config.get_sections():
  55. # Fetch the data for each section.
  56. var player_name = config.get_value(player, "player_name")
  57. var player_score = config.get_value(player, "best_score")
  58. score_data[player_name] = player_score
  59. .. code-tab:: csharp
  60. var score_data = new Godot.Collections.Dictionary();
  61. var config = new ConfigFile();
  62. // Load data from a file.
  63. Error err = config.Load("user://scores.cfg");
  64. // If the file didn't load, ignore it.
  65. if (err != Error.Ok)
  66. {
  67. return;
  68. }
  69. // Iterate over all sections.
  70. foreach (String player in config.GetSections())
  71. {
  72. // Fetch the data for each section.
  73. var player_name = (String)config.GetValue(player, "player_name");
  74. var player_score = (int)config.GetValue(player, "best_score");
  75. score_data[player_name] = player_score;
  76. }
  77. Any operation that mutates the ConfigFile such as :ref:`set_value<class_ConfigFile_method_set_value>`, :ref:`clear<class_ConfigFile_method_clear>`, or :ref:`erase_section<class_ConfigFile_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 :ref:`save<class_ConfigFile_method_save>`, :ref:`save_encrypted<class_ConfigFile_method_save_encrypted>`, or :ref:`save_encrypted_pass<class_ConfigFile_method_save_encrypted_pass>`.
  78. Keep in mind that section and property names can't contain spaces. Anything after a space will be ignored on save and on load.
  79. ConfigFiles can also contain manually written comment lines starting with a semicolon (``;``). 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.
  80. \ **Note:** The file extension given to a ConfigFile does not have any impact on its formatting or behavior. By convention, the ``.cfg`` extension is used here, but any other extension such as ``.ini`` is also valid. Since neither ``.cfg`` nor ``.ini`` are standardized, Godot's ConfigFile formatting may differ from files written by other programs.
  81. .. rst-class:: classref-reftable-group
  82. Methods
  83. -------
  84. .. table::
  85. :widths: auto
  86. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. | |void| | :ref:`clear<class_ConfigFile_method_clear>`\ (\ ) |
  88. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | :ref:`String<class_String>` | :ref:`encode_to_text<class_ConfigFile_method_encode_to_text>`\ (\ ) |const| |
  90. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  91. | |void| | :ref:`erase_section<class_ConfigFile_method_erase_section>`\ (\ section\: :ref:`String<class_String>`\ ) |
  92. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  93. | |void| | :ref:`erase_section_key<class_ConfigFile_method_erase_section_key>`\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`\ ) |
  94. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  95. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`get_section_keys<class_ConfigFile_method_get_section_keys>`\ (\ section\: :ref:`String<class_String>`\ ) |const| |
  96. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  97. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`get_sections<class_ConfigFile_method_get_sections>`\ (\ ) |const| |
  98. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  99. | :ref:`Variant<class_Variant>` | :ref:`get_value<class_ConfigFile_method_get_value>`\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`, default\: :ref:`Variant<class_Variant>` = null\ ) |const| |
  100. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  101. | :ref:`bool<class_bool>` | :ref:`has_section<class_ConfigFile_method_has_section>`\ (\ section\: :ref:`String<class_String>`\ ) |const| |
  102. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  103. | :ref:`bool<class_bool>` | :ref:`has_section_key<class_ConfigFile_method_has_section_key>`\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`\ ) |const| |
  104. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  105. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`load<class_ConfigFile_method_load>`\ (\ path\: :ref:`String<class_String>`\ ) |
  106. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  107. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`load_encrypted<class_ConfigFile_method_load_encrypted>`\ (\ path\: :ref:`String<class_String>`, key\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
  108. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  109. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`load_encrypted_pass<class_ConfigFile_method_load_encrypted_pass>`\ (\ path\: :ref:`String<class_String>`, password\: :ref:`String<class_String>`\ ) |
  110. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  111. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`parse<class_ConfigFile_method_parse>`\ (\ data\: :ref:`String<class_String>`\ ) |
  112. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  113. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`save<class_ConfigFile_method_save>`\ (\ path\: :ref:`String<class_String>`\ ) |
  114. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  115. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`save_encrypted<class_ConfigFile_method_save_encrypted>`\ (\ path\: :ref:`String<class_String>`, key\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
  116. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  117. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`save_encrypted_pass<class_ConfigFile_method_save_encrypted_pass>`\ (\ path\: :ref:`String<class_String>`, password\: :ref:`String<class_String>`\ ) |
  118. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  119. | |void| | :ref:`set_value<class_ConfigFile_method_set_value>`\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`, value\: :ref:`Variant<class_Variant>`\ ) |
  120. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  121. .. rst-class:: classref-section-separator
  122. ----
  123. .. rst-class:: classref-descriptions-group
  124. Method Descriptions
  125. -------------------
  126. .. _class_ConfigFile_method_clear:
  127. .. rst-class:: classref-method
  128. |void| **clear**\ (\ ) :ref:`🔗<class_ConfigFile_method_clear>`
  129. Removes the entire contents of the config.
  130. .. rst-class:: classref-item-separator
  131. ----
  132. .. _class_ConfigFile_method_encode_to_text:
  133. .. rst-class:: classref-method
  134. :ref:`String<class_String>` **encode_to_text**\ (\ ) |const| :ref:`🔗<class_ConfigFile_method_encode_to_text>`
  135. Obtain the text version of this config file (the same text that would be written to a file).
  136. .. rst-class:: classref-item-separator
  137. ----
  138. .. _class_ConfigFile_method_erase_section:
  139. .. rst-class:: classref-method
  140. |void| **erase_section**\ (\ section\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_erase_section>`
  141. Deletes the specified section along with all the key-value pairs inside. Raises an error if the section does not exist.
  142. .. rst-class:: classref-item-separator
  143. ----
  144. .. _class_ConfigFile_method_erase_section_key:
  145. .. rst-class:: classref-method
  146. |void| **erase_section_key**\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_erase_section_key>`
  147. Deletes the specified key in a section. Raises an error if either the section or the key do not exist.
  148. .. rst-class:: classref-item-separator
  149. ----
  150. .. _class_ConfigFile_method_get_section_keys:
  151. .. rst-class:: classref-method
  152. :ref:`PackedStringArray<class_PackedStringArray>` **get_section_keys**\ (\ section\: :ref:`String<class_String>`\ ) |const| :ref:`🔗<class_ConfigFile_method_get_section_keys>`
  153. 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.
  154. .. rst-class:: classref-item-separator
  155. ----
  156. .. _class_ConfigFile_method_get_sections:
  157. .. rst-class:: classref-method
  158. :ref:`PackedStringArray<class_PackedStringArray>` **get_sections**\ (\ ) |const| :ref:`🔗<class_ConfigFile_method_get_sections>`
  159. Returns an array of all defined section identifiers.
  160. .. rst-class:: classref-item-separator
  161. ----
  162. .. _class_ConfigFile_method_get_value:
  163. .. rst-class:: classref-method
  164. :ref:`Variant<class_Variant>` **get_value**\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`, default\: :ref:`Variant<class_Variant>` = null\ ) |const| :ref:`🔗<class_ConfigFile_method_get_value>`
  165. 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 ``default`` value. If ``default`` is not specified or set to ``null``, an error is also raised.
  166. .. rst-class:: classref-item-separator
  167. ----
  168. .. _class_ConfigFile_method_has_section:
  169. .. rst-class:: classref-method
  170. :ref:`bool<class_bool>` **has_section**\ (\ section\: :ref:`String<class_String>`\ ) |const| :ref:`🔗<class_ConfigFile_method_has_section>`
  171. Returns ``true`` if the specified section exists.
  172. .. rst-class:: classref-item-separator
  173. ----
  174. .. _class_ConfigFile_method_has_section_key:
  175. .. rst-class:: classref-method
  176. :ref:`bool<class_bool>` **has_section_key**\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`\ ) |const| :ref:`🔗<class_ConfigFile_method_has_section_key>`
  177. Returns ``true`` if the specified section-key pair exists.
  178. .. rst-class:: classref-item-separator
  179. ----
  180. .. _class_ConfigFile_method_load:
  181. .. rst-class:: classref-method
  182. :ref:`Error<enum_@GlobalScope_Error>` **load**\ (\ path\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_load>`
  183. 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.
  184. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, or one of the other :ref:`Error<enum_@GlobalScope_Error>` values if the operation failed.
  185. .. rst-class:: classref-item-separator
  186. ----
  187. .. _class_ConfigFile_method_load_encrypted:
  188. .. rst-class:: classref-method
  189. :ref:`Error<enum_@GlobalScope_Error>` **load_encrypted**\ (\ path\: :ref:`String<class_String>`, key\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_ConfigFile_method_load_encrypted>`
  190. Loads the encrypted config file specified as a parameter, using the provided ``key`` to decrypt it. The file's contents are parsed and loaded in the **ConfigFile** object which the method was called on.
  191. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, or one of the other :ref:`Error<enum_@GlobalScope_Error>` values if the operation failed.
  192. .. rst-class:: classref-item-separator
  193. ----
  194. .. _class_ConfigFile_method_load_encrypted_pass:
  195. .. rst-class:: classref-method
  196. :ref:`Error<enum_@GlobalScope_Error>` **load_encrypted_pass**\ (\ path\: :ref:`String<class_String>`, password\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_load_encrypted_pass>`
  197. Loads the encrypted config file specified as a parameter, using the provided ``password`` to decrypt it. The file's contents are parsed and loaded in the **ConfigFile** object which the method was called on.
  198. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, or one of the other :ref:`Error<enum_@GlobalScope_Error>` values if the operation failed.
  199. .. rst-class:: classref-item-separator
  200. ----
  201. .. _class_ConfigFile_method_parse:
  202. .. rst-class:: classref-method
  203. :ref:`Error<enum_@GlobalScope_Error>` **parse**\ (\ data\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_parse>`
  204. 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.
  205. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, or one of the other :ref:`Error<enum_@GlobalScope_Error>` values if the operation failed.
  206. .. rst-class:: classref-item-separator
  207. ----
  208. .. _class_ConfigFile_method_save:
  209. .. rst-class:: classref-method
  210. :ref:`Error<enum_@GlobalScope_Error>` **save**\ (\ path\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_save>`
  211. Saves the contents of the **ConfigFile** object to the file specified as a parameter. The output file uses an INI-style structure.
  212. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, or one of the other :ref:`Error<enum_@GlobalScope_Error>` values if the operation failed.
  213. .. rst-class:: classref-item-separator
  214. ----
  215. .. _class_ConfigFile_method_save_encrypted:
  216. .. rst-class:: classref-method
  217. :ref:`Error<enum_@GlobalScope_Error>` **save_encrypted**\ (\ path\: :ref:`String<class_String>`, key\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_ConfigFile_method_save_encrypted>`
  218. Saves the contents of the **ConfigFile** object to the AES-256 encrypted file specified as a parameter, using the provided ``key`` to encrypt it. The output file uses an INI-style structure.
  219. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, or one of the other :ref:`Error<enum_@GlobalScope_Error>` values if the operation failed.
  220. .. rst-class:: classref-item-separator
  221. ----
  222. .. _class_ConfigFile_method_save_encrypted_pass:
  223. .. rst-class:: classref-method
  224. :ref:`Error<enum_@GlobalScope_Error>` **save_encrypted_pass**\ (\ path\: :ref:`String<class_String>`, password\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_save_encrypted_pass>`
  225. Saves the contents of the **ConfigFile** object to the AES-256 encrypted file specified as a parameter, using the provided ``password`` to encrypt it. The output file uses an INI-style structure.
  226. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, or one of the other :ref:`Error<enum_@GlobalScope_Error>` values if the operation failed.
  227. .. rst-class:: classref-item-separator
  228. ----
  229. .. _class_ConfigFile_method_set_value:
  230. .. rst-class:: classref-method
  231. |void| **set_value**\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`, value\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_ConfigFile_method_set_value>`
  232. 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 ``null`` value deletes the specified key if it exists, and deletes the section if it ends up empty once the key has been removed.
  233. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  234. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  235. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  236. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  237. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  238. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  239. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  240. .. |void| replace:: :abbr:`void (No return value.)`