Skip to content

Commit 1be213c

Browse files
committed
GDScript: Update array documentation
* Add info about typed arrays. * Fix the error that packed arrays are passed by value.
1 parent ccae259 commit 1be213c

File tree

1 file changed

+54
-11
lines changed

1 file changed

+54
-11
lines changed

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,10 @@ Built-in types
496496

497497
Built-in types are stack-allocated. They are passed as values. This means a copy
498498
is created on each assignment or when passing them as arguments to functions.
499-
The only exceptions are ``Array``\ s and ``Dictionaries``, which are passed by
500-
reference so they are shared. (Packed arrays such as ``PackedByteArray`` are still
501-
passed as values.)
499+
The exceptions are ``Object``, ``Array``, ``Dictionary``, and packed arrays
500+
(such as ``PackedByteArray``), which are passed by reference so they are shared.
501+
All arrays, ``Dictionary``, and some objects (``Node``, ``Resource``)
502+
have a ``duplicate()`` method that allows you to make a copy.
502503

503504
Basic built-in types
504505
~~~~~~~~~~~~~~~~~~~~
@@ -520,21 +521,21 @@ Short for "boolean", it can only contain ``true`` or ``false``.
520521
^^^^^^^^^^^^^^^^^^^^^^
521522

522523
Short for "integer", it stores whole numbers (positive and negative).
523-
It is stored as a 64-bit value, equivalent to "int64_t" in C++.
524+
It is stored as a 64-bit value, equivalent to ``int64_t`` in C++.
524525

525526
:ref:`float <class_float>`
526527
^^^^^^^^^^^^^^^^^^^^^^^^^^
527528

528529
Stores real numbers, including decimals, using floating-point values.
529-
It is stored as a 64-bit value, equivalent to "double" in C++.
530-
Note: Currently, data structures such as Vector2, Vector3, and
531-
PackedFloat32Array store 32-bit single-precision "float" values.
530+
It is stored as a 64-bit value, equivalent to ``double`` in C++.
531+
Note: Currently, data structures such as ``Vector2``, ``Vector3``, and
532+
``PackedFloat32Array`` store 32-bit single-precision ``float`` values.
532533

533534
:ref:`String <class_String>`
534535
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
535536

536537
A sequence of characters in `Unicode format <https://en.wikipedia.org/wiki/Unicode>`_.
537-
Strings can contain the following escape sequences:
538+
String literals can contain the following escape sequences:
538539

539540
+---------------------+---------------------------------+
540541
| **Escape sequence** | **Expands to** |
@@ -701,6 +702,48 @@ Negative indices count from the end.
701702
arr[0] = "Hi!" # Replacing value 1 with "Hi!".
702703
arr.append(4) # Array is now ["Hi!", 2, 3, 4].
703704

705+
Typed arrays
706+
^^^^^^^^^^^^
707+
708+
Godot 4.0 added support for typed arrays. On write operations, Godot checks that
709+
element values match the specified type, so the array cannot contain invalid values.
710+
The GDScript static analyzer takes typed arrays into account, however array methods like
711+
``front()`` and ``back()`` still have the ``Variant`` return type.
712+
713+
Typed arrays have the syntax ``Array[Type]``, where ``Type`` can be any ``Variant`` type,
714+
native or user class, or enum. Nested array types (like ``Array[Array[int]]``) are not supported.
715+
716+
::
717+
718+
var a: Array[int]
719+
var b: Array[Node]
720+
var c: Array[MyClass]
721+
var d: Array[MyEnum]
722+
var e: Array[Variant]
723+
724+
``Array`` and ``Array[Variant]`` are the same thing.
725+
726+
.. note::
727+
728+
Arrays are passed by reference, so the array element type is also an attribute of the in-memory
729+
structure referenced by a variable in runtime. The static type of a variable restricts the structures
730+
that it can reference to. Therefore, you **cannot** assign an array with a different element type,
731+
even if the type is a subtype of the required type::
732+
733+
var a: Array[Node2D] = [Node2D.new()]
734+
735+
# OK. You can add the value to the array because `Node2D` extends `Node`.
736+
var b: Array[Node] = [a[0]]
737+
738+
# Error. You cannot assign an `Array[Node2D]` to an `Array[Node]` variable.
739+
b = a
740+
741+
The only exception was made for the ``Array`` (``Array[Variant]``) type, for user convenience
742+
and compatibility with old code. However, operations on untyped arrays are considered unsafe.
743+
744+
Packed arrays
745+
^^^^^^^^^^^^^
746+
704747
GDScript arrays are allocated linearly in memory for speed.
705748
Large arrays (more than tens of thousands of elements) may however cause
706749
memory fragmentation. If this is a concern, special types of
@@ -714,9 +757,9 @@ arrays. They are therefore only recommended to use for large data sets:
714757
- :ref:`PackedFloat32Array <class_PackedFloat32Array>`: An array of 32-bit floats.
715758
- :ref:`PackedFloat64Array <class_PackedFloat64Array>`: An array of 64-bit floats.
716759
- :ref:`PackedStringArray <class_PackedStringArray>`: An array of strings.
717-
- :ref:`PackedVector2Array <class_PackedVector2Array>`: An array of :ref:`Vector2 <class_Vector2>` objects.
718-
- :ref:`PackedVector3Array <class_PackedVector3Array>`: An array of :ref:`Vector3 <class_Vector3>` objects.
719-
- :ref:`PackedColorArray <class_PackedColorArray>`: An array of :ref:`Color <class_Color>` objects.
760+
- :ref:`PackedVector2Array <class_PackedVector2Array>`: An array of :ref:`Vector2 <class_Vector2>` values.
761+
- :ref:`PackedVector3Array <class_PackedVector3Array>`: An array of :ref:`Vector3 <class_Vector3>` values.
762+
- :ref:`PackedColorArray <class_PackedColorArray>`: An array of :ref:`Color <class_Color>` values.
720763

721764
:ref:`Dictionary <class_Dictionary>`
722765
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)