Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

ConfigFile

繼承: RefCounted < Object

用於處理 INI 樣式檔的輔助類。

說明

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:

[section]
some_key=42
string_example="Hello World3D!"
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:

# Create new ConfigFile object.
var config = ConfigFile.new()

# 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")

This example shows how the above file could be loaded:

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

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.

方法

void

clear()

String

encode_to_text() const

void

erase_section(section: String)

void

erase_section_key(section: String, key: String)

PackedStringArray

get_section_keys(section: String) const

PackedStringArray

get_sections() const

Variant

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

bool

has_section(section: String) const

bool

has_section_key(section: String, key: String) const

Error

load(path: String)

Error

load_encrypted(path: String, key: PackedByteArray)

Error

load_encrypted_pass(path: String, password: String)

Error

parse(data: String)

Error

save(path: String)

Error

save_encrypted(path: String, key: PackedByteArray)

Error

save_encrypted_pass(path: String, password: String)

void

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


方法說明

void clear() 🔗

移除配置的全部內容。


String encode_to_text() const 🔗

獲得該設定檔的文字版本(與寫入檔的文字相同)。


void erase_section(section: String) 🔗

刪除指定小節以及其中的所有鍵值對。如果該小節不存在,則會引發錯誤。


void erase_section_key(section: String, key: String) 🔗

刪除小節中的指定鍵。如果該小節或鍵不存在,則會引發錯誤。


PackedStringArray get_section_keys(section: String) const 🔗

返回指定小節中所有已定義鍵識別字的陣列。如果該小節不存在,則會引發錯誤並返回一個空陣列。


PackedStringArray get_sections() const 🔗

返回所有已定義小節的識別字的陣列。


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

返回指定小節和鍵的目前值。如果該小節或鍵不存在,則該方法返回後備值 default。如果未指定 default 或將其設定為 null,則會引發一個錯誤。


bool has_section(section: String) const 🔗

如果指定的小節存在,則返回 true


bool has_section_key(section: String, key: String) const 🔗

如果指定的小節-鍵對存在,則返回 true


Error load(path: String) 🔗

載入指定為參數的設定檔。解析檔的內容並將其載入到呼叫該方法的 ConfigFile 對象中。

返回 Error 錯誤碼常數(成功時為 @GlobalScope.OK)。


Error load_encrypted(path: String, key: PackedByteArray) 🔗

載入指定為參數的加密設定檔,使用提供的 key 對其解密。解析檔的內容並將其載入到呼叫該方法的 ConfigFile 物件中。

返回 Error 錯誤碼常數(成功時為 @GlobalScope.OK)。


Error load_encrypted_pass(path: String, password: String) 🔗

載入作為參數的加密設定檔,使用提供的 password 解密。該檔的內容被解析並載入到呼叫該方法的 ConfigFile 物件中。

返回 Error 錯誤碼常數(成功時為 @GlobalScope.OK)。


Error parse(data: String) 🔗

將傳遞的字串解析為設定檔的內容。該字串被解析並載入到呼叫該方法的 ConfigFile 對象中。

返回 Error 錯誤碼常數(成功時為 @GlobalScope.OK)。


Error save(path: String) 🔗

ConfigFile 物件的內容保存到指定為參數的檔中。輸出檔使用 INI 樣式的結構。

返回 Error 錯誤碼常數(成功時為 @GlobalScope.OK)。


Error save_encrypted(path: String, key: PackedByteArray) 🔗

使用提供的 keyConfigFile 物件的內容保存到作為參數指定的 AES-256 加密檔中。輸出檔使用 INI 樣式的結構。

返回 Error 錯誤碼常數(成功時為 @GlobalScope.OK)。


Error save_encrypted_pass(path: String, password: String) 🔗

ConfigFile 物件的內容保存到作為參數指定的 AES-256 加密檔中,使用提供的 password 進行加密。輸出檔使用 INI 風格的結構。

返回 Error 錯誤碼常數(成功時為 @GlobalScope.OK)。


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

為指定小節的指定鍵賦值。如果小節或鍵不存在,則建立它們。如果指定的鍵存在,傳遞 null 值就會移除指定的鍵,如果鍵被移除後,小節最終是空的,就會移除小節。