class_configfile.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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/3.6/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.6/doc/classes/ConfigFile.xml.
  6. .. _class_ConfigFile:
  7. ConfigFile
  8. ==========
  9. **Inherits:** :ref:`Reference<class_Reference>` **<** :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. ::
  16. [section]
  17. some_key=42
  18. string_example="Hello World!"
  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 disk:
  22. ::
  23. # Create new ConfigFile object.
  24. var config = ConfigFile.new()
  25. # Store some values.
  26. config.set_value("Player1", "player_name", "Steve")
  27. config.set_value("Player1", "best_score", 10)
  28. config.set_value("Player2", "player_name", "V3geta")
  29. config.set_value("Player2", "best_score", 9001)
  30. # Save it to a file (overwrite if already exists).
  31. config.save("user://scores.cfg")
  32. This example shows how the above file could be loaded:
  33. ::
  34. var score_data = {}
  35. var config = ConfigFile.new()
  36. # Load data from a file.
  37. var err = config.load("user://scores.cfg")
  38. # If the file didn't load, ignore it.
  39. if err != OK:
  40. return
  41. # Iterate over all sections.
  42. for player in config.get_sections():
  43. # Fetch the data for each section.
  44. var player_name = config.get_value(player, "player_name")
  45. var player_score = config.get_value(player, "best_score")
  46. score_data[player_name] = player_score
  47. 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>`.
  48. Keep in mind that section and property names can't contain spaces. Anything after a space will be ignored on save and on load.
  49. 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.
  50. \ **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.
  51. .. rst-class:: classref-reftable-group
  52. Methods
  53. -------
  54. .. table::
  55. :widths: auto
  56. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | void | :ref:`clear<class_ConfigFile_method_clear>` **(** **)** |
  58. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. | void | :ref:`erase_section<class_ConfigFile_method_erase_section>` **(** :ref:`String<class_String>` section **)** |
  60. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  61. | void | :ref:`erase_section_key<class_ConfigFile_method_erase_section_key>` **(** :ref:`String<class_String>` section, :ref:`String<class_String>` key **)** |
  62. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  63. | :ref:`PoolStringArray<class_PoolStringArray>` | :ref:`get_section_keys<class_ConfigFile_method_get_section_keys>` **(** :ref:`String<class_String>` section **)** |const| |
  64. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`PoolStringArray<class_PoolStringArray>` | :ref:`get_sections<class_ConfigFile_method_get_sections>` **(** **)** |const| |
  66. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`Variant<class_Variant>` | :ref:`get_value<class_ConfigFile_method_get_value>` **(** :ref:`String<class_String>` section, :ref:`String<class_String>` key, :ref:`Variant<class_Variant>` default=null **)** |const| |
  68. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`bool<class_bool>` | :ref:`has_section<class_ConfigFile_method_has_section>` **(** :ref:`String<class_String>` section **)** |const| |
  70. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  71. | :ref:`bool<class_bool>` | :ref:`has_section_key<class_ConfigFile_method_has_section_key>` **(** :ref:`String<class_String>` section, :ref:`String<class_String>` key **)** |const| |
  72. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  73. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`load<class_ConfigFile_method_load>` **(** :ref:`String<class_String>` path **)** |
  74. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  75. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`load_encrypted<class_ConfigFile_method_load_encrypted>` **(** :ref:`String<class_String>` path, :ref:`PoolByteArray<class_PoolByteArray>` key **)** |
  76. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  77. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`load_encrypted_pass<class_ConfigFile_method_load_encrypted_pass>` **(** :ref:`String<class_String>` path, :ref:`String<class_String>` password **)** |
  78. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`parse<class_ConfigFile_method_parse>` **(** :ref:`String<class_String>` data **)** |
  80. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`save<class_ConfigFile_method_save>` **(** :ref:`String<class_String>` path **)** |
  82. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`save_encrypted<class_ConfigFile_method_save_encrypted>` **(** :ref:`String<class_String>` path, :ref:`PoolByteArray<class_PoolByteArray>` key **)** |
  84. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`save_encrypted_pass<class_ConfigFile_method_save_encrypted_pass>` **(** :ref:`String<class_String>` path, :ref:`String<class_String>` password **)** |
  86. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. | void | :ref:`set_value<class_ConfigFile_method_set_value>` **(** :ref:`String<class_String>` section, :ref:`String<class_String>` key, :ref:`Variant<class_Variant>` value **)** |
  88. +-----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. .. rst-class:: classref-section-separator
  90. ----
  91. .. rst-class:: classref-descriptions-group
  92. Method Descriptions
  93. -------------------
  94. .. _class_ConfigFile_method_clear:
  95. .. rst-class:: classref-method
  96. void **clear** **(** **)**
  97. Removes the entire contents of the config.
  98. .. rst-class:: classref-item-separator
  99. ----
  100. .. _class_ConfigFile_method_erase_section:
  101. .. rst-class:: classref-method
  102. void **erase_section** **(** :ref:`String<class_String>` section **)**
  103. Deletes the specified section along with all the key-value pairs inside. Raises an error if the section does not exist.
  104. .. rst-class:: classref-item-separator
  105. ----
  106. .. _class_ConfigFile_method_erase_section_key:
  107. .. rst-class:: classref-method
  108. void **erase_section_key** **(** :ref:`String<class_String>` section, :ref:`String<class_String>` key **)**
  109. Deletes the specified key in a section. Raises an error if either the section or the key do not exist.
  110. .. rst-class:: classref-item-separator
  111. ----
  112. .. _class_ConfigFile_method_get_section_keys:
  113. .. rst-class:: classref-method
  114. :ref:`PoolStringArray<class_PoolStringArray>` **get_section_keys** **(** :ref:`String<class_String>` section **)** |const|
  115. 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.
  116. .. rst-class:: classref-item-separator
  117. ----
  118. .. _class_ConfigFile_method_get_sections:
  119. .. rst-class:: classref-method
  120. :ref:`PoolStringArray<class_PoolStringArray>` **get_sections** **(** **)** |const|
  121. Returns an array of all defined section identifiers.
  122. .. rst-class:: classref-item-separator
  123. ----
  124. .. _class_ConfigFile_method_get_value:
  125. .. rst-class:: classref-method
  126. :ref:`Variant<class_Variant>` **get_value** **(** :ref:`String<class_String>` section, :ref:`String<class_String>` key, :ref:`Variant<class_Variant>` default=null **)** |const|
  127. 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.
  128. .. rst-class:: classref-item-separator
  129. ----
  130. .. _class_ConfigFile_method_has_section:
  131. .. rst-class:: classref-method
  132. :ref:`bool<class_bool>` **has_section** **(** :ref:`String<class_String>` section **)** |const|
  133. Returns ``true`` if the specified section exists.
  134. .. rst-class:: classref-item-separator
  135. ----
  136. .. _class_ConfigFile_method_has_section_key:
  137. .. rst-class:: classref-method
  138. :ref:`bool<class_bool>` **has_section_key** **(** :ref:`String<class_String>` section, :ref:`String<class_String>` key **)** |const|
  139. Returns ``true`` if the specified section-key pair exists.
  140. .. rst-class:: classref-item-separator
  141. ----
  142. .. _class_ConfigFile_method_load:
  143. .. rst-class:: classref-method
  144. :ref:`Error<enum_@GlobalScope_Error>` **load** **(** :ref:`String<class_String>` path **)**
  145. 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.
  146. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  147. .. rst-class:: classref-item-separator
  148. ----
  149. .. _class_ConfigFile_method_load_encrypted:
  150. .. rst-class:: classref-method
  151. :ref:`Error<enum_@GlobalScope_Error>` **load_encrypted** **(** :ref:`String<class_String>` path, :ref:`PoolByteArray<class_PoolByteArray>` key **)**
  152. 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.
  153. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  154. .. rst-class:: classref-item-separator
  155. ----
  156. .. _class_ConfigFile_method_load_encrypted_pass:
  157. .. rst-class:: classref-method
  158. :ref:`Error<enum_@GlobalScope_Error>` **load_encrypted_pass** **(** :ref:`String<class_String>` path, :ref:`String<class_String>` password **)**
  159. 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.
  160. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  161. .. rst-class:: classref-item-separator
  162. ----
  163. .. _class_ConfigFile_method_parse:
  164. .. rst-class:: classref-method
  165. :ref:`Error<enum_@GlobalScope_Error>` **parse** **(** :ref:`String<class_String>` data **)**
  166. 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.
  167. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  168. .. rst-class:: classref-item-separator
  169. ----
  170. .. _class_ConfigFile_method_save:
  171. .. rst-class:: classref-method
  172. :ref:`Error<enum_@GlobalScope_Error>` **save** **(** :ref:`String<class_String>` path **)**
  173. Saves the contents of the **ConfigFile** object to the file specified as a parameter. The output file uses an INI-style structure.
  174. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  175. .. rst-class:: classref-item-separator
  176. ----
  177. .. _class_ConfigFile_method_save_encrypted:
  178. .. rst-class:: classref-method
  179. :ref:`Error<enum_@GlobalScope_Error>` **save_encrypted** **(** :ref:`String<class_String>` path, :ref:`PoolByteArray<class_PoolByteArray>` key **)**
  180. 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.
  181. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  182. .. rst-class:: classref-item-separator
  183. ----
  184. .. _class_ConfigFile_method_save_encrypted_pass:
  185. .. rst-class:: classref-method
  186. :ref:`Error<enum_@GlobalScope_Error>` **save_encrypted_pass** **(** :ref:`String<class_String>` path, :ref:`String<class_String>` password **)**
  187. 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.
  188. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  189. .. rst-class:: classref-item-separator
  190. ----
  191. .. _class_ConfigFile_method_set_value:
  192. .. rst-class:: classref-method
  193. void **set_value** **(** :ref:`String<class_String>` section, :ref:`String<class_String>` key, :ref:`Variant<class_Variant>` value **)**
  194. 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.
  195. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  196. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  197. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  198. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`