1
0
mirror of https://github.com/godotengine/godot.git synced 2026-01-05 19:31:35 +00:00

Document typed dictionaries and arrays in the class reference

(cherry picked from commit b6dcb111d2)
This commit is contained in:
Hugo Locurcio
2025-06-03 02:23:12 +02:00
committed by Thaddeus Crews
parent 49d2dbc027
commit 4aedc06168
2 changed files with 43 additions and 2 deletions

View File

@@ -94,7 +94,7 @@
pointsDict["Blue"] = 150; // Add "Blue" as a key and assign 150 as its value.
[/csharp]
[/codeblocks]
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:
[codeblocks]
[gdscript]
# This is a valid dictionary.
@@ -133,6 +133,39 @@
}
[/csharp]
[/codeblocks]
To enforce a certain type for keys and values, you can create a [i]typed dictionary[/i]. Typed dictionaries can only contain keys and values of the given types, or that inherit from the given classes:
[codeblocks]
[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",
}
[/gdscript]
[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"},
};
[/csharp]
[/codeblocks]
[b]Note:[/b] Dictionaries are always passed by reference. To get a copy of a dictionary which can be modified independently of the original dictionary, use [method duplicate].
[b]Note:[/b] Erasing elements while iterating over dictionaries is [b]not[/b] supported and will result in unpredictable behavior.
</description>