ConfigFile

Inherits: Reference < Object

类别: 核心

简要说明

用于处理ini样式文件的帮助程序类。

方法

无效

erase_section ( String section )

PoolStringArray

get_section_keys ( String section ) const

PoolStringArray

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, PoolByteArray key )

Error

load_encrypted_pass ( String path, String pass )

Error

save ( String path )

Error

save_encrypted ( String path, PoolByteArray key )

Error

save_encrypted_pass ( String path, String pass )

无效

set_value ( String section, String key, Variant value )

描述

此帮助程序类可用于存储 Variant 文件系统上使用ini样式格式的值。存储的值由节和键标识:

[section]
some_key=42
string_example="Hello World!"
a_vector=Vector3( 1, 0, 2 )

存储的数据可以保存到文件中,也可以从文件中进行分析,尽管也可以直接使用configfile对象,而无需访问文件系统。

下面的示例演示如何从系统中解析一个ini样式的文件,读取其内容并在其中存储新值:

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

请记住,节和属性名称不能包含空格。在保存和加载时,空间之后的任何内容都将被忽略。

方法说明

  • void erase_section ( String section )

删除指定的节以及其中的所有键值对。

返回指定节中所有已定义键标识符的数组。

返回所有已定义节标识符的数组。

返回指定节和键的当前值。如果节和/或键不存在,该方法将返回可选的 default 论点,或 null 如果省略。

返回 true 如果指定的节存在。

返回 true 如果指定的节密钥对存在。

加载指定为参数的配置文件。文件的内容被解析并加载到调用该方法的configFile对象中。返回 @GlobalScope.OK@GlobalScope.FAILEDERR_* 中列出的常量 @GlobalScope . 如果加载成功,返回值为 @GlobalScope.OK .

将configFile对象的内容保存到指定为参数的文件中。输出文件使用ini样式的结构。返回 @GlobalScope.OK@GlobalScope.FAILEDERR_* 中列出的常量 @GlobalScope . 如果加载成功,返回值为 @GlobalScope.OK .

为指定节的指定键赋值。如果节和/或键不存在,则创建它们。通过 null 值删除指定的键(如果存在),如果该键被删除,则删除该节(如果该节最后为空)。