mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-04 14:11:02 +03:00
classref: Sync with current master branch (cb7cd81)
This commit is contained in:
@@ -134,7 +134,7 @@ To add a key to an existing dictionary, access it like an existing key and assig
|
||||
|
||||
|
||||
|
||||
Finally, dictionaries can contain different types of keys and values in the same dictionary:
|
||||
Finally, untyped dictionaries can contain different types of keys and values in the same dictionary:
|
||||
|
||||
|
||||
.. tabs::
|
||||
@@ -185,6 +185,45 @@ The keys of a dictionary can be iterated with the ``for`` keyword:
|
||||
|
||||
|
||||
|
||||
To enforce a certain type for keys and values, you can create a *typed dictionary*. Typed dictionaries can only contain keys and values of the given types, or that inherit from the given classes:
|
||||
|
||||
|
||||
.. tabs::
|
||||
|
||||
.. code-tab:: gdscript
|
||||
|
||||
# Creates a typed dictionary with String keys and int values.
|
||||
# Attempting to use any other type for keys or values will result in an error.
|
||||
var typed_dict: Dictionary[String, int] = {
|
||||
"some_key": 1,
|
||||
"some_other_key": 2,
|
||||
}
|
||||
|
||||
# Creates a typed dictionary with String keys and values of any type.
|
||||
# Attempting to use any other type for keys will result in an error.
|
||||
var typed_dict_key_only: Dictionary[String, Variant] = {
|
||||
"some_key": 12.34,
|
||||
"some_other_key": "string",
|
||||
}
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
// Creates a typed dictionary with String keys and int values.
|
||||
// Attempting to use any other type for keys or values will result in an error.
|
||||
var typedDict = new Godot.Collections.Dictionary<String, int> {
|
||||
{"some_key", 1},
|
||||
{"some_other_key", 2},
|
||||
};
|
||||
|
||||
// Creates a typed dictionary with String keys and values of any type.
|
||||
// Attempting to use any other type for keys will result in an error.
|
||||
var typedDictKeyOnly = new Godot.Collections.Dictionary<String, Variant> {
|
||||
{"some_key", 12.34},
|
||||
{"some_other_key", "string"},
|
||||
};
|
||||
|
||||
|
||||
|
||||
\ **Note:** Dictionaries are always passed by reference. To get a copy of a dictionary which can be modified independently of the original dictionary, use :ref:`duplicate()<class_Dictionary_method_duplicate>`.
|
||||
|
||||
\ **Note:** Erasing elements while iterating over dictionaries is **not** supported and will result in unpredictable behavior.
|
||||
@@ -855,7 +894,7 @@ Returns ``true`` if the two dictionaries contain the same keys and values, inner
|
||||
|
||||
:ref:`bool<class_bool>` **set**\ (\ key\: :ref:`Variant<class_Variant>`, value\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_Dictionary_method_set>`
|
||||
|
||||
Sets the value of the element at the given ``key`` to the given ``value``. This is the same as using the ``[]`` operator (``array[index] = value``).
|
||||
Sets the value of the element at the given ``key`` to the given ``value``. This is the same as using the ``[]`` operator (``dict[key] = value``).
|
||||
|
||||
.. rst-class:: classref-item-separator
|
||||
|
||||
|
||||
Reference in New Issue
Block a user