Skip to content

Commit f293509

Browse files
authored
Merge pull request #10095 from Calinou/static-typing-array-typed
Clarify restrictions on nested array types in Static typing in GDScript
2 parents d2e0797 + a878406 commit f293509

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

tutorials/scripting/gdscript/static_typing.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,17 @@ To define the type of an ``Array``, enclose the type name in ``[]``.
198198
An array's type applies to ``for`` loop variables, as well as some operators like
199199
``[]``, ``[]=``, and ``+``. Array methods (such as ``push_back``) and other operators
200200
(such as ``==``) are still untyped. Built-in types, native and custom classes,
201-
and enums may be used as element types. Nested array types are not supported.
201+
and enums may be used as element types. Nested array types
202+
(like ``Array[Array[int]]``) are not supported.
203+
202204

203205
::
204206

205207
var scores: Array[int] = [10, 20, 30]
206208
var vehicles: Array[Node] = [$Car, $Plane]
207209
var items: Array[Item] = [Item.new()]
208-
# var arrays: Array[Array] -- disallowed
210+
var array_of_arrays: Array[Array] = [[], []]
211+
# var arrays: Array[Array[int]] -- disallowed
209212

210213
for score in scores:
211214
# score has type `int`
@@ -407,10 +410,10 @@ that has a script attached with ``class_name MyScript`` and that ``extends
407410
Node2D``. If we have a reference to the object as a ``Node2D`` (for instance,
408411
as it was passed to us by the physics system), we can first check if the
409412
property and method exist and then set and call them if they do::
410-
413+
411414
if "some_property" in node_2d:
412415
node_2d.some_property = 20 # Produces UNSAFE_PROPERTY_ACCESS warning.
413-
416+
414417
if node_2d.has_method("some_function"):
415418
node_2d.some_function() # Produces UNSAFE_METHOD_ACCESS warning.
416419

@@ -420,7 +423,7 @@ in the referenced type - in this case a ``Node2D``. To make these operations
420423
safe, you can first check if the object is of type ``MyScript`` using the
421424
``is`` keyword and then declare a variable with the type ``MyScript`` on
422425
which you can set its properties and call its methods::
423-
426+
424427
if node_2d is MyScript:
425428
var my_script: MyScript = node_2d
426429
my_script.some_property = 20
@@ -443,7 +446,7 @@ collision area to show the area's name. Once the object enters the collision
443446
area, the physics system sends a signal with a ``Node2D`` object, and the most
444447
straightforward (but not statically typed) solution to do what we want could
445448
be achieved like this::
446-
449+
447450
func _on_body_entered(body: Node2D) -> void:
448451
body.label.text = name # Produces UNSAFE_PROPERTY_ACCESS warning.
449452

0 commit comments

Comments
 (0)