Up to date

This page is up to date for Godot 4.1. If you still find outdated information, please open an issue.

ConfigFile

Inherits: RefCounted < Object

Helper class to handle INI-style files.

Description

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:

  1. [section]
  2. some_key=42
  3. string_example="Hello World3D!"
  4. a_vector=Vector3(1, 0, 2)

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 create a simple ConfigFile and save it on disc:

GDScriptC#

  1. # Create new ConfigFile object.
  2. var config = ConfigFile.new()
  3. # Store some values.
  4. config.set_value("Player1", "player_name", "Steve")
  5. config.set_value("Player1", "best_score", 10)
  6. config.set_value("Player2", "player_name", "V3geta")
  7. config.set_value("Player2", "best_score", 9001)
  8. # Save it to a file (overwrite if already exists).
  9. config.save("user://scores.cfg")
  1. // Create new ConfigFile object.
  2. var config = new ConfigFile();
  3. // Store some values.
  4. config.SetValue("Player1", "player_name", "Steve");
  5. config.SetValue("Player1", "best_score", 10);
  6. config.SetValue("Player2", "player_name", "V3geta");
  7. config.SetValue("Player2", "best_score", 9001);
  8. // Save it to a file (overwrite if already exists).
  9. config.Save("user://scores.cfg");

This example shows how the above file could be loaded:

GDScriptC#

  1. var score_data = {}
  2. var config = ConfigFile.new()
  3. # Load data from a file.
  4. var err = config.load("user://scores.cfg")
  5. # If the file didn't load, ignore it.
  6. if err != OK:
  7. return
  8. # Iterate over all sections.
  9. for player in config.get_sections():
  10. # Fetch the data for each section.
  11. var player_name = config.get_value(player, "player_name")
  12. var player_score = config.get_value(player, "best_score")
  13. score_data[player_name] = player_score
  1. var score_data = new Godot.Collections.Dictionary();
  2. var config = new ConfigFile();
  3. // Load data from a file.
  4. Error err = config.Load("user://scores.cfg");
  5. // If the file didn't load, ignore it.
  6. if (err != Error.Ok)
  7. {
  8. return;
  9. }
  10. // Iterate over all sections.
  11. foreach (String player in config.GetSections())
  12. {
  13. // Fetch the data for each section.
  14. var player_name = (String)config.GetValue(player, "player_name");
  15. var player_score = (int)config.GetValue(player, "best_score");
  16. score_data[player_name] = player_score;
  17. }

Any operation that mutates the ConfigFile such as set_value, clear, or 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 save, save_encrypted, or save_encrypted_pass.

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 (;). 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.

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.

Methods

void

clear ( )

String

encode_to_text ( ) const

void

erase_section ( String section )

void

erase_section_key ( String section, String key )

PackedStringArray

get_section_keys ( String section ) const

PackedStringArray

get_sections ( ) const

Variant

get_value ( String section, String key, Variant default=null ) const

bool

has_section ( String section ) const

bool

has_section_key ( String section, String key ) const

Error

load ( String path )

Error

load_encrypted ( String path, PackedByteArray key )

Error

load_encrypted_pass ( String path, String password )

Error

parse ( String data )

Error

save ( String path )

Error

save_encrypted ( String path, PackedByteArray key )

Error

save_encrypted_pass ( String path, String password )

void

set_value ( String section, String key, Variant value )


Method Descriptions

void clear ( )

Removes the entire contents of the config.


String encode_to_text ( ) const

Obtain the text version of this config file (the same text that would be written to a file).


void erase_section ( String section )

Deletes the specified section along with all the key-value pairs inside. Raises an error if the section does not exist.


void erase_section_key ( String section, String key )

Deletes the specified key in a section. Raises an error if either the section or the key do not exist.


PackedStringArray get_section_keys ( String section ) const

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.


PackedStringArray get_sections ( ) const

Returns an array of all defined section identifiers.


Variant get_value ( String section, String key, Variant default=null ) const

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.


bool has_section ( String section ) const

Returns true if the specified section exists.


bool has_section_key ( String section, String key ) const

Returns true if the specified section-key pair exists.


Error load ( String path )

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 Error code constants (@GlobalScope.OK on success).


Error load_encrypted ( String path, PackedByteArray key )

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.

Returns one of the Error code constants (@GlobalScope.OK on success).


Error load_encrypted_pass ( String path, String password )

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.

Returns one of the Error code constants (@GlobalScope.OK on success).


Error parse ( String data )

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.

Returns one of the Error code constants (@GlobalScope.OK on success).


Error save ( String path )

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 Error code constants (@GlobalScope.OK on success).


Error save_encrypted ( String path, PackedByteArray key )

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.

Returns one of the Error code constants (@GlobalScope.OK on success).


Error save_encrypted_pass ( String path, String password )

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.

Returns one of the Error code constants (@GlobalScope.OK on success).


void set_value ( String section, String key, Variant value )

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.