Improve ConfigFile example

(cherry picked from commit 1721f0143e)
This commit is contained in:
kobewi 2021-07-26 16:24:08 +02:00 committed by Rémi Verschelde
parent dec840452d
commit 4bc527fedd
No known key found for this signature in database
GPG key ID: C3336907360768E1

View file

@ -12,18 +12,38 @@
a_vector=Vector3( 1, 0, 2 )
[/codeblock]
The stored data can be saved to or parsed from a file, though ConfigFile objects can also be used directly without accessing the filesystem.
The following example shows how to parse an INI-style file from the system, read its contents and store new values in it:
The following example shows how to create a simple [ConfigFile] and save it on disk:
[codeblock]
# Create new ConfigFile object.
var config = ConfigFile.new()
var err = config.load("user://settings.cfg")
if err == OK: # If not, something went wrong with the file loading
# Look for the display/width pair, and default to 1024 if missing
var screen_width = config.get_value("display", "width", 1024)
# Store a variable if and only if it hasn't been defined yet
if not config.has_section_key("audio", "mute"):
config.set_value("audio", "mute", false)
# Save the changes by overwriting the previous file
config.save("user://settings.cfg")
# Store some values.
config.set_value("Player1", "player_name", "Steve")
config.set_value("Player1", "best_score", 10)
config.set_value("Player2", "player_name", "V3geta")
config.set_value("Player2", "best_score", 9001)
# Save it to a file (overwrite if already exists).
config.save("user://scores.cfg")
[/codeblock]
This example shows how the above file could be loaded:
[codeblock]
var score_data = {}
var config = ConfigFile.new()
# Load data from a file.
var err = config.load("user://scores.cfg")
# If the file didn't load, ignore it.
if err != OK:
return
# Iterate over all sections.
for player in config.get_sections():
# Fetch the data for each section.
var player_name = config.get_value(player, "player_name")
var player_score = config.get_value(player, "best_score")
score_data[player_name] = player_score
[/codeblock]
Keep in mind that section and property names can't contain spaces. Anything after a space will be ignored on save and on load.
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.