1
0
mirror of https://github.com/godotengine/godot.git synced 2025-12-06 17:25:19 +00:00

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 811ea6ad64
commit 5692bc8dd0

View File

@@ -12,18 +12,38 @@
a_vector=Vector3( 1, 0, 2 ) a_vector=Vector3( 1, 0, 2 )
[/codeblock] [/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 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] [codeblock]
# Create new ConfigFile object.
var config = ConfigFile.new() var config = ConfigFile.new()
var err = config.load("user://settings.cfg")
if err == OK: # If not, something went wrong with the file loading # Store some values.
# Look for the display/width pair, and default to 1024 if missing config.set_value("Player1", "player_name", "Steve")
var screen_width = config.get_value("display", "width", 1024) config.set_value("Player1", "best_score", 10)
# Store a variable if and only if it hasn't been defined yet config.set_value("Player2", "player_name", "V3geta")
if not config.has_section_key("audio", "mute"): config.set_value("Player2", "best_score", 9001)
config.set_value("audio", "mute", false)
# Save the changes by overwriting the previous file # Save it to a file (overwrite if already exists).
config.save("user://settings.cfg") 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] [/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. 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. 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.