Skip to content

Commit 7b7f4a4

Browse files
committed
Document typed dictionaries in GDScript
1 parent 1ce1ede commit 7b7f4a4

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

tutorials/scripting/gdscript/gdscript_basics.rst

+27
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,33 @@ assign to it::
979979
this, use the :ref:`Object.get() <class_Object_method_get>` and
980980
:ref:`Object.set() <class_Object_method_set>` methods instead.
981981

982+
Typed dictionaries
983+
^^^^^^^^^^^^^^^^^^
984+
985+
Godot 4.4 added support for typed dictionaries. On write operations, Godot checks that
986+
element keys and values match the specified type, so the dictionary cannot contain invalid
987+
keys or values. The GDScript static analyzer takes typed dictionaries into account. However,
988+
dictionary methods that return values still have the ``Variant`` return type.
989+
990+
Typed dictionaries have the syntax ``Array[KeyType, ValueType]``, where ``KeyType`` and ``ValueType``
991+
can be any ``Variant`` type, native or user class, or enum. Both the key and value type **must** be specified,
992+
but you can use ``Variant`` to make either of them untyped.
993+
Nested dictionary types (like ``Dictionary[String, Dictionary[String, int]]``)
994+
are not supported.
995+
996+
::
997+
998+
var a: Dictionary[String, int]
999+
var b: Dictionary[String, Node]
1000+
var c: Dictionary[Vector2i, MyClass]
1001+
var d: Dictionary[MyEnum, float]
1002+
# String keys, values can be any type.
1003+
var e: Dictionary[String, Variant]
1004+
# Keys can be any type, boolean values.
1005+
var f: Dictionary[Variant, bool]
1006+
1007+
``Dictionary`` and ``Dictionary[Variant, Variant]`` are the same thing.
1008+
9821009
:ref:`Signal <class_Signal>`
9831010
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9841011

tutorials/scripting/gdscript/static_typing.rst

+29
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,35 @@ For instance, you can write::
230230
The array will remain untyped, but the ``name`` variable within the ``for`` loop
231231
will always be of ``String`` type.
232232

233+
Specify the element type of a ``Dictionary``
234+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
235+
236+
To define the type of a ``Dictionary``'s keys and values, enclose the type name in ``[]``
237+
and separate the key and value type with a comma.
238+
239+
A dictionary's value type applies to ``for`` loop variables, as well as some operators like
240+
``[]`` and ``[]=``. Dictionary methods that return values and other operators
241+
(such as ``==``) are still untyped. Built-in types, native and custom classes,
242+
and enums may be used as element types. Nested array types
243+
(like ``Dictionary[String, Dictionary[String, int]]``) are not supported.
244+
245+
246+
::
247+
248+
var fruit_costs: Dictionary[String, int] = { "apple": 5, "orange": 10 }
249+
var vehicles: Dictionary[String, Node] = { "car": $Car, "plane": $Plane }
250+
var item_tiles: Dictionary[Vector2i, Item] = { Vector2i(0, 0): Item.new(), Vector2i(0, 1): Item.new() }
251+
var dictionary_of_dictionaries: Dictionary[String, Dictionary] = { { } }
252+
# var arrays: Dictionary[String, Dictionary[String, int]] -- disallowed
253+
254+
for cost in fruit_costs:
255+
# cost has type `int`
256+
257+
# The following would be errors:
258+
fruit_costs["pear"] += vehicles
259+
var s: String = fruit_costs["apple"]
260+
fruit_costs["orange"] = "lots"
261+
233262
Type casting
234263
~~~~~~~~~~~~
235264

0 commit comments

Comments
 (0)