Skip to content

Commit 64867d4

Browse files
authored
Merge pull request godotengine#7812 from dalexeev/gds-mention-array-assign-method
GDScript: Mention `Array.assign()` method in typed arrays documentation
2 parents 15bd2cc + 34a620e commit 64867d4

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,16 +728,23 @@ native or user class, or enum. Nested array types (like ``Array[Array[int]]``) a
728728
Arrays are passed by reference, so the array element type is also an attribute of the in-memory
729729
structure referenced by a variable in runtime. The static type of a variable restricts the structures
730730
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::
731+
even if the type is a subtype of the required type.
732+
733+
If you want to *convert* a typed array, you can create a new array and use the
734+
:ref:`Array.assign() <class_Array_method_assign>` method::
732735

733736
var a: Array[Node2D] = [Node2D.new()]
734737

735-
# OK. You can add the value to the array because `Node2D` extends `Node`.
738+
# (OK) You can add the value to the array because `Node2D` extends `Node`.
736739
var b: Array[Node] = [a[0]]
737740

738-
# Error. You cannot assign an `Array[Node2D]` to an `Array[Node]` variable.
741+
# (Error) You cannot assign an `Array[Node2D]` to an `Array[Node]` variable.
739742
b = a
740743

744+
# (OK) But you can use the `assign()` method instead. Unlike the `=` operator,
745+
# the `assign()` method copies the contents of the array, not the reference.
746+
b.assign(a)
747+
741748
The only exception was made for the ``Array`` (``Array[Variant]``) type, for user convenience
742749
and compatibility with old code. However, operations on untyped arrays are considered unsafe.
743750

0 commit comments

Comments
 (0)