Dictionaries are associative containers that contain values referenced by unique keys. Dictionaries will preserve the insertion order when adding new entries. In other programming languages, this data structure is often referred to as a hash map or an associative array.
You can define a dictionary by placing a comma-separated list of key: value
pairs inside curly braces {}
.
Creating a dictionary:
You can access a dictionary's value by referencing its corresponding key. In the above example, points_dict["White"]
will return 50
. You can also write points_dict.White
, which is equivalent. However, you'll have to use the bracket syntax if the key you're accessing the dictionary with isn't a fixed string (such as a number or variable).
In the above code, points
will be assigned the value that is paired with the appropriate color selected in my_color
.
Dictionaries can contain more complex data:
To add a key to an existing dictionary, access it like an existing key and assign to it:
Finally, dictionaries can contain different types of keys and values in the same dictionary:
The keys of a dictionary can be iterated with the for
keyword:
Constructs an empty Dictionary.
Creates a typed dictionary from the base
dictionary. A typed dictionary can only contain keys and values of the given types, or that inherit from the given classes, as described by this constructor's parameters.
Returns the same dictionary as from
. If you need a copy of the dictionary, use duplicate().
Returns true
if the two dictionaries do not contain the same keys and values.
Returns true
if the two dictionaries contain the same keys and values. The order of the entries does not matter.
Note: In C#, by convention, this operator compares by reference. If you need to compare by value, iterate over both dictionaries.
Returns the corresponding value for the given key
in the dictionary. If the entry does not exist, fails and returns null
. For safe access, use get() or has().
Assigns elements of another dictionary
into the dictionary. Resizes the dictionary to match dictionary
. Performs type conversions if the dictionary is typed.
Clears the dictionary, removing all entries from it.
Creates and returns a new copy of the dictionary. If deep
is true
, inner Dictionary and Array keys and values are also copied, recursively.
Removes the dictionary entry by key, if it exists. Returns true
if the given key
existed in the dictionary, otherwise false
.
Note: Do not erase entries while iterating over the dictionary. You can iterate over the keys() array instead.
Finds and returns the first key whose associated value is equal to value
, or null
if it is not found.
Note: null
is also a valid key. If inside the dictionary, find_key() may give misleading results.
Returns the corresponding value for the given key
in the dictionary. If the key
does not exist, returns default
, or null
if the parameter is omitted.
Gets a value and ensures the key is set. If the key
exists in the dictionary, this behaves like get(). Otherwise, the default
value is inserted into the dictionary and returned.
Returns the built-in Variant type of the typed dictionary's keys as a Variant.Type constant. If the keys are not typed, returns @GlobalScope.TYPE_NIL. See also is_typed_key().
Returns the built-in class name of the typed dictionary's keys, if the built-in Variant type is @GlobalScope.TYPE_OBJECT. Otherwise, returns an empty StringName. See also is_typed_key() and Object.get_class().
Returns the Script instance associated with this typed dictionary's keys, or null
if it does not exist. See also is_typed_key().
Returns the built-in Variant type of the typed dictionary's values as a Variant.Type constant. If the values are not typed, returns @GlobalScope.TYPE_NIL. See also is_typed_value().
Returns the built-in class name of the typed dictionary's values, if the built-in Variant type is @GlobalScope.TYPE_OBJECT. Otherwise, returns an empty StringName. See also is_typed_value() and Object.get_class().
Returns the Script instance associated with this typed dictionary's values, or null
if it does not exist. See also is_typed_value().
Returns true
if the dictionary contains an entry with the given key
.
In GDScript, this is equivalent to the in
operator:
Note: This method returns true
as long as the key
exists, even if its corresponding value is null
.
Returns true
if the dictionary contains all keys in the given keys
array.
Returns a hashed 32-bit integer value representing the dictionary contents.
Note: Dictionaries with the same entries but in a different order will not have the same hash.
Note: Dictionaries with equal hash values are not guaranteed to be the same, because of hash collisions. On the contrary, dictionaries with different hash values are guaranteed to be different.
Returns true
if the dictionary is empty (its size is 0
). See also size().
Returns true
if the dictionary is read-only. See make_read_only(). Dictionaries are automatically read-only if declared with const
keyword.
Returns true
if the dictionary is typed the same as dictionary
.
Returns true
if the dictionary's keys are typed the same as dictionary
's keys.
Returns true
if the dictionary's values are typed the same as dictionary
's values.
Returns true
if the dictionary is typed. Typed dictionaries can only store keys/values of their associated type and provide type safety for the []
operator. Methods of typed dictionary still return Variant.
Returns true
if the dictionary's keys are typed.
Returns true
if the dictionary's values are typed.
Returns the list of keys in the dictionary.
Makes the dictionary read-only, i.e. disables modification of the dictionary's contents. Does not apply to nested content, e.g. content of nested dictionaries.
Adds entries from dictionary
to this dictionary. By default, duplicate keys are not copied over, unless overwrite
is true
.
Note: merge() is not recursive. Nested dictionaries are considered as keys that can be overwritten or not depending on the value of overwrite
, but they will never be merged together.
Returns a copy of this dictionary merged with the other dictionary
. By default, duplicate keys are not copied over, unless overwrite
is true
. See also merge().
This method is useful for quickly making dictionaries with default values:
Returns true
if the two dictionaries contain the same keys and values, inner Dictionary and Array keys and values are compared recursively.
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
).
Returns the number of entries in the dictionary. Empty dictionaries ({ }
) always return 0
. See also is_empty().
Sorts the dictionary in-place by key. This can be used to ensure dictionaries with the same contents produce equivalent results when getting the keys(), getting the values(), and converting to a string. This is also useful when wanting a JSON representation consistent with what is in memory, and useful for storing on a database that requires dictionaries to be sorted.
Returns the list of values in this dictionary.