Skip to content

Update Dictionary class reference about typed dictionaries #106520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion doc/classes/Dictionary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

var points_dict = {"White": 50, "Yellow": 75, "Orange": 100}

var typed_dict: Dictionary[int, String] = {} # Creates a typed dictionary
var typed_key: Dictionary[int, Variant] = {}
var typed_value: Dictionary[Variant, int] = {}

# Alternative Lua-style syntax.
# Doesn't require quotes around keys, but only string constants can be used as key names.
# Additionally, key names must start with a letter or an underscore.
Expand All @@ -36,6 +40,9 @@
{"Yellow", 75},
{"Orange", 100}
};
var typedDict = new Godot.Collections.Dictionary<int,String>();
var typedKey = new Godot.Collections.Dictionary<int,Variant>();
var typedValue = new Godot.Collections.Dictionary<Variant,int>();
[/csharp]
[/codeblocks]
You can access a dictionary's value by referencing its corresponding key. In the above example, [code]points_dict["White"][/code] will return [code]50[/code]. You can also write [code]points_dict.White[/code], 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).
Expand Down Expand Up @@ -94,7 +101,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.
Expand Down Expand Up @@ -159,6 +166,23 @@
<param index="6" name="value_script" type="Variant" />
<description>
Creates a typed dictionary from the [param 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.
The type of keys and values are set through the key/value [code]_type[/code], [code]_class_name[/code] and [code]_script[/code] parameters:
Key type properties:
- [param key_type] is the [Variant] type of the key, as one of the [enum Variant.Type] constants.
- [param key_class_name] is the class name of the key.
- [param key_script] is the assigned script. It must be a [Script] instance or [code]null[/code].
Value type properties:
- [param value_type] is the [Variant] type of the key, as one of the [enum Variant.Type] constants.
- [param key_class_name] is the class name of the key.
- [param key_script] is the assigned script. It must be a [Script] instance or [code]null[/code].
If [code]key/value_type[/code] is not [constant TYPE_OBJECT], the corresponding [code]class_name[/code] must be an empty [StringName] and [code]script[/code] must be [code]null[/code].
The [param base] dictionary's elements are converted when necessary. If this is not possible or [param base] is already typed, this constructor fails and returns an empty [Dictionary].
In GDScript, you can also create a typed dictionary using static typing:
[codeblock]
# Both create a typed dictionary with integer keys and string values
var constructor = Dictionary({},TYPE_INT,"",null,TYPE_STRING,"",null)
var static_typing: Dictionary[int,String] = {}
[/codeblock]
</description>
</constructor>
<constructor name="Dictionary">
Expand Down
Loading