mirror of
https://github.com/godotengine/godot-docs-l10n.git
synced 2026-01-04 10:09:56 +03:00
396 lines
21 KiB
ReStructuredText
396 lines
21 KiB
ReStructuredText
:github_url: hide
|
|
|
|
.. _class_ConfigFile:
|
|
|
|
ConfigFile
|
|
==========
|
|
|
|
**繼承:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
|
|
|
|
用於處理 INI 樣式檔的輔助類。
|
|
|
|
.. rst-class:: classref-introduction-group
|
|
|
|
說明
|
|
----
|
|
|
|
This helper class can be used to store :ref:`Variant<class_Variant>` values on the filesystem using INI-style formatting. The stored values are identified by a section and a key:
|
|
|
|
.. code:: text
|
|
|
|
[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:
|
|
|
|
|
|
.. tabs::
|
|
|
|
.. code-tab:: gdscript
|
|
|
|
# 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")
|
|
|
|
.. code-tab:: csharp
|
|
|
|
// Create new ConfigFile object.
|
|
var config = new ConfigFile();
|
|
|
|
// Store some values.
|
|
config.SetValue("Player1", "player_name", "Steve");
|
|
config.SetValue("Player1", "best_score", 10);
|
|
config.SetValue("Player2", "player_name", "V3geta");
|
|
config.SetValue("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:
|
|
|
|
|
|
.. tabs::
|
|
|
|
.. code-tab:: gdscript
|
|
|
|
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
|
|
|
|
.. code-tab:: csharp
|
|
|
|
var score_data = new Godot.Collections.Dictionary();
|
|
var config = new ConfigFile();
|
|
|
|
// Load data from a file.
|
|
Error err = config.Load("user://scores.cfg");
|
|
|
|
// If the file didn't load, ignore it.
|
|
if (err != Error.Ok)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Iterate over all sections.
|
|
foreach (String player in config.GetSections())
|
|
{
|
|
// Fetch the data for each section.
|
|
var player_name = (String)config.GetValue(player, "player_name");
|
|
var player_score = (int)config.GetValue(player, "best_score");
|
|
score_data[player_name] = player_score;
|
|
}
|
|
|
|
|
|
|
|
Any operation that mutates the ConfigFile such as :ref:`set_value()<class_ConfigFile_method_set_value>`, :ref:`clear()<class_ConfigFile_method_clear>`, or :ref:`erase_section()<class_ConfigFile_method_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 :ref:`save()<class_ConfigFile_method_save>`, :ref:`save_encrypted()<class_ConfigFile_method_save_encrypted>`, or :ref:`save_encrypted_pass()<class_ConfigFile_method_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.
|
|
|
|
.. rst-class:: classref-reftable-group
|
|
|
|
方法
|
|
----
|
|
|
|
.. table::
|
|
:widths: auto
|
|
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| |void| | :ref:`clear<class_ConfigFile_method_clear>`\ (\ ) |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`String<class_String>` | :ref:`encode_to_text<class_ConfigFile_method_encode_to_text>`\ (\ ) |const| |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| |void| | :ref:`erase_section<class_ConfigFile_method_erase_section>`\ (\ section\: :ref:`String<class_String>`\ ) |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| |void| | :ref:`erase_section_key<class_ConfigFile_method_erase_section_key>`\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`\ ) |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`get_section_keys<class_ConfigFile_method_get_section_keys>`\ (\ section\: :ref:`String<class_String>`\ ) |const| |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`get_sections<class_ConfigFile_method_get_sections>`\ (\ ) |const| |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Variant<class_Variant>` | :ref:`get_value<class_ConfigFile_method_get_value>`\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`, default\: :ref:`Variant<class_Variant>` = null\ ) |const| |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`bool<class_bool>` | :ref:`has_section<class_ConfigFile_method_has_section>`\ (\ section\: :ref:`String<class_String>`\ ) |const| |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`bool<class_bool>` | :ref:`has_section_key<class_ConfigFile_method_has_section_key>`\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`\ ) |const| |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`load<class_ConfigFile_method_load>`\ (\ path\: :ref:`String<class_String>`\ ) |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`load_encrypted<class_ConfigFile_method_load_encrypted>`\ (\ path\: :ref:`String<class_String>`, key\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`load_encrypted_pass<class_ConfigFile_method_load_encrypted_pass>`\ (\ path\: :ref:`String<class_String>`, password\: :ref:`String<class_String>`\ ) |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`parse<class_ConfigFile_method_parse>`\ (\ data\: :ref:`String<class_String>`\ ) |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`save<class_ConfigFile_method_save>`\ (\ path\: :ref:`String<class_String>`\ ) |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`save_encrypted<class_ConfigFile_method_save_encrypted>`\ (\ path\: :ref:`String<class_String>`, key\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`save_encrypted_pass<class_ConfigFile_method_save_encrypted_pass>`\ (\ path\: :ref:`String<class_String>`, password\: :ref:`String<class_String>`\ ) |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| |void| | :ref:`set_value<class_ConfigFile_method_set_value>`\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`, value\: :ref:`Variant<class_Variant>`\ ) |
|
|
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
|
|
.. rst-class:: classref-section-separator
|
|
|
|
----
|
|
|
|
.. rst-class:: classref-descriptions-group
|
|
|
|
方法說明
|
|
--------
|
|
|
|
.. _class_ConfigFile_method_clear:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
|void| **clear**\ (\ ) :ref:`🔗<class_ConfigFile_method_clear>`
|
|
|
|
移除配置的全部內容。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_encode_to_text:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`String<class_String>` **encode_to_text**\ (\ ) |const| :ref:`🔗<class_ConfigFile_method_encode_to_text>`
|
|
|
|
獲得該設定檔的文字版本(與寫入檔的文字相同)。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_erase_section:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
|void| **erase_section**\ (\ section\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_erase_section>`
|
|
|
|
刪除指定小節以及其中的所有鍵值對。如果該小節不存在,則會引發錯誤。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_erase_section_key:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
|void| **erase_section_key**\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_erase_section_key>`
|
|
|
|
刪除小節中的指定鍵。如果該小節或鍵不存在,則會引發錯誤。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_get_section_keys:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`PackedStringArray<class_PackedStringArray>` **get_section_keys**\ (\ section\: :ref:`String<class_String>`\ ) |const| :ref:`🔗<class_ConfigFile_method_get_section_keys>`
|
|
|
|
返回指定小節中所有已定義鍵識別字的陣列。如果該小節不存在,則會引發錯誤並返回一個空陣列。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_get_sections:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`PackedStringArray<class_PackedStringArray>` **get_sections**\ (\ ) |const| :ref:`🔗<class_ConfigFile_method_get_sections>`
|
|
|
|
返回所有已定義小節的識別字的陣列。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_get_value:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Variant<class_Variant>` **get_value**\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`, default\: :ref:`Variant<class_Variant>` = null\ ) |const| :ref:`🔗<class_ConfigFile_method_get_value>`
|
|
|
|
返回指定小節和鍵的目前值。如果該小節或鍵不存在,則該方法返回後備值 ``default``\ 。如果未指定 ``default`` 或將其設定為 ``null``\ ,則會引發一個錯誤。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_has_section:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`bool<class_bool>` **has_section**\ (\ section\: :ref:`String<class_String>`\ ) |const| :ref:`🔗<class_ConfigFile_method_has_section>`
|
|
|
|
如果指定的小節存在,則返回 ``true``\ 。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_has_section_key:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`bool<class_bool>` **has_section_key**\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`\ ) |const| :ref:`🔗<class_ConfigFile_method_has_section_key>`
|
|
|
|
如果指定的小節-鍵對存在,則返回 ``true``\ 。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_load:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Error<enum_@GlobalScope_Error>` **load**\ (\ path\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_load>`
|
|
|
|
載入指定為參數的設定檔。解析檔的內容並將其載入到呼叫該方法的 **ConfigFile** 對象中。
|
|
|
|
返回 :ref:`Error<enum_@GlobalScope_Error>` 錯誤碼常數(成功時為 :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>`\ )。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_load_encrypted:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Error<enum_@GlobalScope_Error>` **load_encrypted**\ (\ path\: :ref:`String<class_String>`, key\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_ConfigFile_method_load_encrypted>`
|
|
|
|
載入指定為參數的加密設定檔,使用提供的 ``key`` 對其解密。解析檔的內容並將其載入到呼叫該方法的 **ConfigFile** 物件中。
|
|
|
|
返回 :ref:`Error<enum_@GlobalScope_Error>` 錯誤碼常數(成功時為 :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>`\ )。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_load_encrypted_pass:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Error<enum_@GlobalScope_Error>` **load_encrypted_pass**\ (\ path\: :ref:`String<class_String>`, password\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_load_encrypted_pass>`
|
|
|
|
載入作為參數的加密設定檔,使用提供的 ``password`` 解密。該檔的內容被解析並載入到呼叫該方法的 **ConfigFile** 物件中。
|
|
|
|
返回 :ref:`Error<enum_@GlobalScope_Error>` 錯誤碼常數(成功時為 :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>`\ )。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_parse:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Error<enum_@GlobalScope_Error>` **parse**\ (\ data\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_parse>`
|
|
|
|
將傳遞的字串解析為設定檔的內容。該字串被解析並載入到呼叫該方法的 ConfigFile 對象中。
|
|
|
|
返回 :ref:`Error<enum_@GlobalScope_Error>` 錯誤碼常數(成功時為 :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>`\ )。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_save:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Error<enum_@GlobalScope_Error>` **save**\ (\ path\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_save>`
|
|
|
|
將 **ConfigFile** 物件的內容保存到指定為參數的檔中。輸出檔使用 INI 樣式的結構。
|
|
|
|
返回 :ref:`Error<enum_@GlobalScope_Error>` 錯誤碼常數(成功時為 :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>`\ )。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_save_encrypted:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Error<enum_@GlobalScope_Error>` **save_encrypted**\ (\ path\: :ref:`String<class_String>`, key\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_ConfigFile_method_save_encrypted>`
|
|
|
|
使用提供的 ``key`` 將 **ConfigFile** 物件的內容保存到作為參數指定的 AES-256 加密檔中。輸出檔使用 INI 樣式的結構。
|
|
|
|
返回 :ref:`Error<enum_@GlobalScope_Error>` 錯誤碼常數(成功時為 :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>`\ )。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_save_encrypted_pass:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Error<enum_@GlobalScope_Error>` **save_encrypted_pass**\ (\ path\: :ref:`String<class_String>`, password\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ConfigFile_method_save_encrypted_pass>`
|
|
|
|
將 **ConfigFile** 物件的內容保存到作為參數指定的 AES-256 加密檔中,使用提供的 ``password`` 進行加密。輸出檔使用 INI 風格的結構。
|
|
|
|
返回 :ref:`Error<enum_@GlobalScope_Error>` 錯誤碼常數(成功時為 :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>`\ )。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_ConfigFile_method_set_value:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
|void| **set_value**\ (\ section\: :ref:`String<class_String>`, key\: :ref:`String<class_String>`, value\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_ConfigFile_method_set_value>`
|
|
|
|
為指定小節的指定鍵賦值。如果小節或鍵不存在,則建立它們。如果指定的鍵存在,傳遞 ``null`` 值就會移除指定的鍵,如果鍵被移除後,小節最終是空的,就會移除小節。
|
|
|
|
.. |virtual| replace:: :abbr:`virtual (本方法通常需要使用者覆寫才能生效。)`
|
|
.. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)`
|
|
.. |const| replace:: :abbr:`const (本方法沒有副作用。不會修改該實例的任何成員變數。)`
|
|
.. |vararg| replace:: :abbr:`vararg (本方法除了這裡描述的參數外,還可以接受任意數量的參數。)`
|
|
.. |constructor| replace:: :abbr:`constructor (本方法用於建構一個型別。)`
|
|
.. |static| replace:: :abbr:`static (本方法無需實例即可呼叫,因此可以直接使用類別名稱呼叫。)`
|
|
.. |operator| replace:: :abbr:`operator (本方法描述將本型別作為左運算元時可用的有效運算子。)`
|
|
.. |bitfield| replace:: :abbr:`BitField (此值是由下列旗標組成的位元遮罩整數。)`
|
|
.. |void| replace:: :abbr:`void (無回傳值。)`
|