An array data structure that can contain a sequence of elements of any Variant type. Elements are accessed by a numerical index starting at 0
. Negative indices are used to count from the back (-1
is the last element, -2
is the second to last, etc.).
Differences between packed arrays, typed arrays, and untyped arrays: Packed arrays are generally faster to iterate on and modify compared to a typed array of the same type (e.g. PackedInt64Array versus Array[int]
). Also, packed arrays consume less memory. As a downside, packed arrays are less flexible as they don't offer as many convenience methods such as map(). Typed arrays are in turn faster to iterate on and modify than untyped arrays.
Constructs an empty Array.
Creates a typed array from the base
array. A typed array can only contain elements of the given type, or that inherit from the given class, as described by this constructor's parameters:
type
is the built-in Variant type, as one the Variant.Type constants.
class_name
is the built-in class name (see Object.get_class()).
script
is the associated script. It must be a Script instance or null
.
If type
is not @GlobalScope.TYPE_OBJECT, class_name
must be an empty StringName and script
must be null
.
The base
array's elements are converted when necessary. If this is not possible or base
is already typed, this constructor fails and returns an empty Array.
In GDScript, this constructor is usually not necessary, as it is possible to create a typed array through static typing:
Returns the same array as from
. If you need a copy of the array, use duplicate().
Constructs an array from a PackedByteArray.
Constructs an array from a PackedColorArray.
Constructs an array from a PackedFloat32Array.
Constructs an array from a PackedFloat64Array.
Constructs an array from a PackedInt32Array.
Constructs an array from a PackedInt64Array.
Constructs an array from a PackedStringArray.
Constructs an array from a PackedVector2Array.
Constructs an array from a PackedVector3Array.
Constructs an array from a PackedVector4Array.
Returns true
if the array's size or its elements are different than right
's.
Appends the right
array to the left operand, creating a new Array. This is also known as an array concatenation.
Note: For existing arrays, append_array() is much more efficient than concatenation and assignment with the +=
operator.
Compares the elements of both arrays in order, starting from index 0
and ending on the last index in common between both arrays. For each pair of elements, returns true
if this array's element is less than right
's, false
if this element is greater. Otherwise, continues to the next pair.
If all searched elements are equal, returns true
if this array's size is less than right
's, otherwise returns false
.
Compares the elements of both arrays in order, starting from index 0
and ending on the last index in common between both arrays. For each pair of elements, returns true
if this array's element is less than right
's, false
if this element is greater. Otherwise, continues to the next pair.
If all searched elements are equal, returns true
if this array's size is less or equal to right
's, otherwise returns false
.
Compares the left operand Array against the right
Array. Returns true
if the sizes and contents of the arrays are equal, false
otherwise.
Compares the elements of both arrays in order, starting from index 0
and ending on the last index in common between both arrays. For each pair of elements, returns true
if this array's element is greater than right
's, false
if this element is less. Otherwise, continues to the next pair.
If all searched elements are equal, returns true
if this array's size is greater than right
's, otherwise returns false
.
Compares the elements of both arrays in order, starting from index 0
and ending on the last index in common between both arrays. For each pair of elements, returns true
if this array's element is greater than right
's, false
if this element is less. Otherwise, continues to the next pair.
If all searched elements are equal, returns true
if this array's size is greater or equal to right
's, otherwise returns false
.
Returns the Variant element at the specified index
. Arrays start at index 0. If index
is greater or equal to 0
, the element is fetched starting from the beginning of the array. If index
is a negative value, the element is fetched starting from the end. Accessing an array out-of-bounds will cause a run-time error, pausing the project execution if run from the editor.
Calls the given Callable on each element in the array and returns true
if the Callable returns true
for all elements in the array. If the Callable returns false
for one array element or more, this method returns false
.
The method
should take one Variant parameter (the current array element) and return a bool.
See also any(), filter(), map() and reduce().
Note: Unlike relying on the size of an array returned by filter(), this method will return as early as possible to improve performance (especially with large arrays).
Note: For an empty array, this method always returns true
.
Calls the given Callable on each element in the array and returns true
if the Callable returns true
for one or more elements in the array. If the Callable returns false
for all elements in the array, this method returns false
.
The method
should take one Variant parameter (the current array element) and return a bool.
See also all(), filter(), map() and reduce().
Note: Unlike relying on the size of an array returned by filter(), this method will return as early as possible to improve performance (especially with large arrays).
Note: For an empty array, this method always returns false
.
Appends value
at the end of the array (alias of push_back()).
Appends another array
at the end of this array.
Assigns elements of another array
into the array. Resizes the array to match array
. Performs type conversions if the array is typed.
Returns the last element of the array. If the array is empty, fails and returns null
. See also front().
Note: Unlike with the []
operator (array[-1]
), an error is generated without stopping project execution.
Returns the index of value
in the sorted array. If it cannot be found, returns where value
should be inserted to keep the array sorted. The algorithm used is binary search.
If before
is true
(as by default), the returned index comes before all existing elements equal to value
in the array.
Note: Calling bsearch() on an unsorted array will result in unexpected behavior. Use sort() before calling this method.
Returns the index of value
in the sorted array. If it cannot be found, returns where value
should be inserted to keep the array sorted (using func
for the comparisons). The algorithm used is binary search.
Similar to sort_custom(), func
is called as many times as necessary, receiving one array element and value
as arguments. The function should return true
if the array element should be behind value
, otherwise it should return false
.
If before
is true
(as by default), the returned index comes before all existing elements equal to value
in the array.
Note: Calling bsearch_custom() on an unsorted array will result in unexpected behavior. Use sort_custom() with func
before calling this method.
Removes all elements from the array. This is equivalent to using resize() with a size of 0
.
Returns the number of times an element is in the array.
To count how many elements in an array satisfy a condition, see reduce().
Returns a new copy of the array.
By default, a shallow copy is returned: all nested Array and Dictionary elements are shared with the original array. Modifying them in one array will also affect them in the other.
If deep
is true
, a deep copy is returned: all nested arrays and dictionaries are also duplicated (recursively).
Finds and removes the first occurrence of value
from the array. If value
does not exist in the array, nothing happens. To remove an element by index, use remove_at() instead.
Note: This method shifts every element's index after the removed value
back, which may have a noticeable performance cost, especially on larger arrays.
Note: Erasing elements while iterating over arrays is not supported and will result in unpredictable behavior.
Assigns the given value
to all elements in the array.
This method can often be combined with resize() to create an array with a given size and initialized elements:
Note: If value
is a Variant passed by reference (Object-derived, Array, Dictionary, etc.), the array will be filled with references to the same value
, which are not duplicates.
Calls the given Callable on each element in the array and returns a new, filtered Array.
The method
receives one of the array elements as an argument, and should return true
to add the element to the filtered array, or false
to exclude it.
Returns the index of the first occurrence of what
in this array, or -1
if there are none. The search's start can be specified with from
, continuing to the end of the array.
Note: If you just want to know whether the array contains what
, use has() (Contains
in C#). In GDScript, you may also use the in
operator.
Note: For performance reasons, the search is affected by what
's Variant.Type. For example, 7
(int) and 7.0
(float) are not considered equal for this method.
Returns the index of the first element in the array that causes method
to return true
, or -1
if there are none. The search's start can be specified with from
, continuing to the end of the array.
method
is a callable that takes an element of the array, and returns a bool.
Note: If you just want to know whether the array contains anything that satisfies method
, use any().
Returns the first element of the array. If the array is empty, fails and returns null
. See also back().
Note: Unlike with the []
operator (array[0]
), an error is generated without stopping project execution.
Returns the element at the given index
in the array. This is the same as using the []
operator (array[index]
).
Returns the built-in Variant type of the typed array as a Variant.Type constant. If the array is not typed, returns @GlobalScope.TYPE_NIL. See also is_typed().
Returns the built-in class name of the typed array, if the built-in Variant type @GlobalScope.TYPE_OBJECT. Otherwise, returns an empty StringName. See also is_typed() and Object.get_class().
Returns the Script instance associated with this typed array, or null
if it does not exist. See also is_typed().
Returns true
if the array contains the given value
.
In GDScript, this is equivalent to the in
operator:
Note: For performance reasons, the search is affected by the value
's Variant.Type. For example, 7
(int) and 7.0
(float) are not considered equal for this method.
Returns a hashed 32-bit integer value representing the array and its contents.
Note: Arrays with equal hash values are not guaranteed to be the same, as a result of hash collisions. On the countrary, arrays with different hash values are guaranteed to be different.
Inserts a new element (value
) at a given index (position
) in the array. position
should be between 0
and the array's size().
Returns @GlobalScope.OK on success, or one of the other Error constants if this method fails.
Note: Every element's index after position
needs to be shifted forward, which may have a noticeable performance cost, especially on larger arrays.
Returns true
if the array is empty ([]
). See also size().
Returns true
if the array is read-only. See make_read_only().
In GDScript, arrays are automatically read-only if declared with the const
keyword.
Returns true
if this array is typed the same as the given array
. See also is_typed().
Returns true
if the array is typed. Typed arrays can only contain elements of a specific type, as defined by the typed array constructor. The methods of a typed array are still expected to return a generic Variant.
In GDScript, it is possible to define a typed array with static typing:
Makes the array read-only. The array's elements cannot be overridden with different values, and their order cannot change. Does not apply to nested elements, such as dictionaries.
In GDScript, arrays are automatically read-only if declared with the const
keyword.
Calls the given Callable for each element in the array and returns a new array filled with values returned by the method
.
The method
should take one Variant parameter (the current array element) and can return any Variant.
Returns the maximum value contained in the array, if all elements can be compared. Otherwise, returns null
. See also min().
To find the maximum value using a custom comparator, you can use reduce().
Returns the minimum value contained in the array, if all elements can be compared. Otherwise, returns null
. See also max().
Returns a random element from the array. Generates an error and returns null
if the array is empty.
Note: Like many similar functions in the engine (such as @GlobalScope.randi() or shuffle()), this method uses a common, global random seed. To get a predictable outcome from this method, see @GlobalScope.seed().
Removes and returns the element of the array at index position
. If negative, position
is considered relative to the end of the array. Returns null
if the array is empty. If position
is out of bounds, an error message is also generated.
Note: This method shifts every element's index after position
back, which may have a noticeable performance cost, especially on larger arrays.
Removes and returns the last element of the array. Returns null
if the array is empty, without generating an error. See also pop_front().
Removes and returns the first element of the array. Returns null
if the array is empty, without generating an error. See also pop_back().
Note: This method shifts every other element's index back, which may have a noticeable performance cost, especially on larger arrays.
Appends an element at the end of the array. See also push_front().
Adds an element at the beginning of the array. See also push_back().
Note: This method shifts every other element's index forward, which may have a noticeable performance cost, especially on larger arrays.
Calls the given Callable for each element in array, accumulates the result in accum
, then returns it.
The method
takes two arguments: the current value of accum
and the current array element. If accum
is null
(as by default), the iteration will start from the second element, with the first one used as initial value of accum
.
If max() is not desirable, this method may also be used to implement a custom comparator:
This method can also be used to count how many elements in an array satisfy a certain condition, similar to count():
Removes the element from the array at the given index (position
). If the index is out of bounds, this method fails.
If you need to return the removed element, use pop_at(). To remove an element by value, use erase() instead.
Note: This method shifts every element's index after position
back, which may have a noticeable performance cost, especially on larger arrays.
Note: The position
cannot be negative. To remove an element relative to the end of the array, use arr.remove_at(arr.size() - (i + 1))
. To remove the last element from the array, use arr.resize(arr.size() - 1)
.
Sets the array's number of elements to size
. If size
is smaller than the array's current size, the elements at the end are removed. If size
is greater, new default elements (usually null
) are added, depending on the array's type.
Returns @GlobalScope.OK on success, or one of the other Error constants if this method fails.
Note: Calling this method once and assigning the new values is faster than calling append() for every new element.
Reverses the order of all elements in the array.
Returns the index of the last occurrence of what
in this array, or -1
if there are none. The search's start can be specified with from
, continuing to the beginning of the array. This method is the reverse of find().
Returns the index of the last element of the array that causes method
to return true
, or -1
if there are none. The search's start can be specified with from
, continuing to the beginning of the array. This method is the reverse of find_custom().
Sets the value of the element at the given index
to the given value
. This will not change the size of the array, it only changes the value at an index already in the array. This is the same as using the []
operator (array[index] = value
).
Shuffles all elements of the array in a random order.
Note: Like many similar functions in the engine (such as @GlobalScope.randi() or pick_random()), this method uses a common, global random seed. To get a predictable outcome from this method, see @GlobalScope.seed().
Returns the number of elements in the array. Empty arrays ([]
) always return 0
. See also is_empty().
Returns a new Array containing this array's elements, from index begin
(inclusive) to end
(exclusive), every step
elements.
If either begin
or end
are negative, their value is relative to the end of the array.
If step
is negative, this method iterates through the array in reverse, returning a slice ordered backwards. For this to work, begin
must be greater than end
.
If deep
is true
, all nested Array and Dictionary elements in the slice are duplicated from the original, recursively. See also duplicate()).
Sorts the array in ascending order. The final order is dependent on the "less than" (<
) comparison between elements.
Note: The sorting algorithm used is not stable. This means that equivalent elements (such as 2
and 2.0
) may have their order changed when calling sort().
Sorts the array using a custom Callable.
func
is called as many times as necessary, receiving two array elements as arguments. The function should return true
if the first element should be moved before the second one, otherwise it should return false
.
It may also be necessary to use this method to sort strings by natural order, with String.naturalnocasecmp_to(), as in the following example:
Note: In C#, this method is not supported.
Note: The sorting algorithm used is not stable. This means that values considered equal may have their order changed when calling this method.
Note: You should not randomize the return value of func
, as the heapsort algorithm expects a consistent result. Randomizing the return value will result in unexpected behavior.