ConfigFile.xml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="ConfigFile" inherits="Reference" category="Core" version="3.1">
  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. <demos>
  33. </demos>
  34. <methods>
  35. <method name="erase_section">
  36. <return type="void">
  37. </return>
  38. <argument index="0" name="section" type="String">
  39. </argument>
  40. <description>
  41. Deletes the specified section along with all the key-value pairs inside.
  42. </description>
  43. </method>
  44. <method name="get_section_keys" qualifiers="const">
  45. <return type="PoolStringArray">
  46. </return>
  47. <argument index="0" name="section" type="String">
  48. </argument>
  49. <description>
  50. Returns an array of all defined key identifiers in the specified section.
  51. </description>
  52. </method>
  53. <method name="get_sections" qualifiers="const">
  54. <return type="PoolStringArray">
  55. </return>
  56. <description>
  57. Returns an array of all defined section identifiers.
  58. </description>
  59. </method>
  60. <method name="get_value" qualifiers="const">
  61. <return type="Variant">
  62. </return>
  63. <argument index="0" name="section" type="String">
  64. </argument>
  65. <argument index="1" name="key" type="String">
  66. </argument>
  67. <argument index="2" name="default" type="Variant" default="null">
  68. </argument>
  69. <description>
  70. 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.
  71. </description>
  72. </method>
  73. <method name="has_section" qualifiers="const">
  74. <return type="bool">
  75. </return>
  76. <argument index="0" name="section" type="String">
  77. </argument>
  78. <description>
  79. Returns [code]true[/code] if the specified section exists.
  80. </description>
  81. </method>
  82. <method name="has_section_key" qualifiers="const">
  83. <return type="bool">
  84. </return>
  85. <argument index="0" name="section" type="String">
  86. </argument>
  87. <argument index="1" name="key" type="String">
  88. </argument>
  89. <description>
  90. Returns [code]true[/code] if the specified section-key pair exists.
  91. </description>
  92. </method>
  93. <method name="load">
  94. <return type="int" enum="Error">
  95. </return>
  96. <argument index="0" name="path" type="String">
  97. </argument>
  98. <description>
  99. 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 [code]OK[/code], [code]FAILED[/code] or [code]ERR_*[/code] constants listed in [@GlobalScope]. If the load was successful, the return value is [code]OK[/code].
  100. </description>
  101. </method>
  102. <method name="save">
  103. <return type="int" enum="Error">
  104. </return>
  105. <argument index="0" name="path" type="String">
  106. </argument>
  107. <description>
  108. 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 [code]OK[/code], [code]FAILED[/code] or [code]ERR_*[/code] constants listed in [@GlobalScope]. If the load was successful, the return value is [code]OK[/code].
  109. </description>
  110. </method>
  111. <method name="set_value">
  112. <return type="void">
  113. </return>
  114. <argument index="0" name="section" type="String">
  115. </argument>
  116. <argument index="1" name="key" type="String">
  117. </argument>
  118. <argument index="2" name="value" type="Variant">
  119. </argument>
  120. <description>
  121. 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.
  122. </description>
  123. </method>
  124. </methods>
  125. <constants>
  126. </constants>
  127. </class>