ConfigFile.xml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="ConfigFile" inherits="Reference" category="Core" version="3.1.2">
  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]
  9. [section]
  10. some_key=42
  11. string_example="Hello World!"
  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 parse an INI-style file from the system, read its contents and store new values in it:
  16. [codeblock]
  17. var config = ConfigFile.new()
  18. var err = config.load("user://settings.cfg")
  19. if err == OK: # if not, something went wrong with the file loading
  20. # Look for the display/width pair, and default to 1024 if missing
  21. var screen_width = config.get_value("display", "width", 1024)
  22. # Store a variable if and only if it hasn't been defined yet
  23. if not config.has_section_key("audio", "mute"):
  24. config.set_value("audio", "mute", false)
  25. # Save the changes by overwriting the previous file
  26. config.save("user://settings.cfg")
  27. [/codeblock]
  28. Keep in mind that section and property names can't contain spaces. Anything after a space will be ignored on save and on load.
  29. </description>
  30. <tutorials>
  31. </tutorials>
  32. <methods>
  33. <method name="erase_section">
  34. <return type="void">
  35. </return>
  36. <argument index="0" name="section" type="String">
  37. </argument>
  38. <description>
  39. Deletes the specified section along with all the key-value pairs inside.
  40. </description>
  41. </method>
  42. <method name="get_section_keys" qualifiers="const">
  43. <return type="PoolStringArray">
  44. </return>
  45. <argument index="0" name="section" type="String">
  46. </argument>
  47. <description>
  48. Returns an array of all defined key identifiers in the specified section.
  49. </description>
  50. </method>
  51. <method name="get_sections" qualifiers="const">
  52. <return type="PoolStringArray">
  53. </return>
  54. <description>
  55. Returns an array of all defined section identifiers.
  56. </description>
  57. </method>
  58. <method name="get_value" qualifiers="const">
  59. <return type="Variant">
  60. </return>
  61. <argument index="0" name="section" type="String">
  62. </argument>
  63. <argument index="1" name="key" type="String">
  64. </argument>
  65. <argument index="2" name="default" type="Variant" default="null">
  66. </argument>
  67. <description>
  68. Returns the current value for the specified section and key. If the section and/or the key do not exist, the method returns the value of the optional [code]default[/code] argument, or [code]null[/code] if it is omitted.
  69. </description>
  70. </method>
  71. <method name="has_section" qualifiers="const">
  72. <return type="bool">
  73. </return>
  74. <argument index="0" name="section" type="String">
  75. </argument>
  76. <description>
  77. Returns [code]true[/code] if the specified section exists.
  78. </description>
  79. </method>
  80. <method name="has_section_key" qualifiers="const">
  81. <return type="bool">
  82. </return>
  83. <argument index="0" name="section" type="String">
  84. </argument>
  85. <argument index="1" name="key" type="String">
  86. </argument>
  87. <description>
  88. Returns [code]true[/code] if the specified section-key pair exists.
  89. </description>
  90. </method>
  91. <method name="load">
  92. <return type="int" enum="Error">
  93. </return>
  94. <argument index="0" name="path" type="String">
  95. </argument>
  96. <description>
  97. 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. Returns one of the [constant @GlobalScope.OK], [constant @GlobalScope.FAILED] or [code]ERR_*[/code] constants listed in [@GlobalScope]. If the load was successful, the return value is [constant @GlobalScope.OK].
  98. </description>
  99. </method>
  100. <method name="save">
  101. <return type="int" enum="Error">
  102. </return>
  103. <argument index="0" name="path" type="String">
  104. </argument>
  105. <description>
  106. Saves the contents of the ConfigFile object to the file specified as a parameter. The output file uses an INI-style structure. Returns one of the [constant @GlobalScope.OK], [constant @GlobalScope.FAILED] or [code]ERR_*[/code] constants listed in [@GlobalScope]. If the load was successful, the return value is [constant @GlobalScope.OK].
  107. </description>
  108. </method>
  109. <method name="set_value">
  110. <return type="void">
  111. </return>
  112. <argument index="0" name="section" type="String">
  113. </argument>
  114. <argument index="1" name="key" type="String">
  115. </argument>
  116. <argument index="2" name="value" type="Variant">
  117. </argument>
  118. <description>
  119. Assigns a value to the specified key of the specified section. If the section and/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.
  120. </description>
  121. </method>
  122. </methods>
  123. <constants>
  124. </constants>
  125. </class>