github_url: | hide |
---|
A built-in data structure that holds a sequence of elements.
.. rst-class:: classref-introduction-group
An array data structure that can contain a sequence of elements of any 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.).
Example:
.. tabs:: .. code-tab:: gdscript var array = ["One", 2, 3, "Four"] print(array[0]) # One. print(array[2]) # 3. print(array[-1]) # Four. array[2] = "Three" print(array[-2]) # Three. .. code-tab:: csharp var array = new Godot.Collections.Array{"One", 2, 3, "Four"}; GD.Print(array[0]); // One. GD.Print(array[2]); // 3. GD.Print(array[array.Count - 1]); // Four. array[2] = "Three"; GD.Print(array[array.Count - 2]); // Three.
Arrays can be concatenated using the +
operator:
.. tabs:: .. code-tab:: gdscript var array1 = ["One", 2] var array2 = [3, "Four"] print(array1 + array2) # ["One", 2, 3, "Four"] .. code-tab:: csharp // Array concatenation is not possible with C# arrays, but is with Godot.Collections.Array. var array1 = new Godot.Collections.Array{"One", 2}; var array2 = new Godot.Collections.Array{3, "Four"}; GD.Print(array1 + array2); // Prints [One, 2, 3, Four]
Note: Arrays are always passed by reference. To get a copy of an array that can be modified independently of the original array, use :ref:`duplicate<class_Array_method_duplicate>`.
Note: Erasing elements while iterating over arrays is not supported and will result in unpredictable behavior.
Note
There are notable differences when using this API with C#. See :ref:`doc_c_sharp_differences` for more information.
.. rst-class:: classref-reftable-group
.. rst-class:: classref-reftable-group
.. rst-class:: classref-reftable-group
.. rst-class:: classref-section-separator
.. rst-class:: classref-descriptions-group
.. rst-class:: classref-constructor
:ref:`Array<class_Array>` Array ( )
Constructs an empty Array.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Array<class_Array>` Array ( :ref:`Array<class_Array>` base, :ref:`int<class_int>` type, :ref:`StringName<class_StringName>` class_name, :ref:`Variant<class_Variant>` script )
Creates a typed array from the base
array.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Array<class_Array>` Array ( :ref:`Array<class_Array>` from )
Returns the same array as from
. If you need a copy of the array, use :ref:`duplicate<class_Array_method_duplicate>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Array<class_Array>` Array ( :ref:`PackedByteArray<class_PackedByteArray>` from )
Constructs an array from a :ref:`PackedByteArray<class_PackedByteArray>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Array<class_Array>` Array ( :ref:`PackedColorArray<class_PackedColorArray>` from )
Constructs an array from a :ref:`PackedColorArray<class_PackedColorArray>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Array<class_Array>` Array ( :ref:`PackedFloat32Array<class_PackedFloat32Array>` from )
Constructs an array from a :ref:`PackedFloat32Array<class_PackedFloat32Array>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Array<class_Array>` Array ( :ref:`PackedFloat64Array<class_PackedFloat64Array>` from )
Constructs an array from a :ref:`PackedFloat64Array<class_PackedFloat64Array>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Array<class_Array>` Array ( :ref:`PackedInt32Array<class_PackedInt32Array>` from )
Constructs an array from a :ref:`PackedInt32Array<class_PackedInt32Array>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Array<class_Array>` Array ( :ref:`PackedInt64Array<class_PackedInt64Array>` from )
Constructs an array from a :ref:`PackedInt64Array<class_PackedInt64Array>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Array<class_Array>` Array ( :ref:`PackedStringArray<class_PackedStringArray>` from )
Constructs an array from a :ref:`PackedStringArray<class_PackedStringArray>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Array<class_Array>` Array ( :ref:`PackedVector2Array<class_PackedVector2Array>` from )
Constructs an array from a :ref:`PackedVector2Array<class_PackedVector2Array>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Array<class_Array>` Array ( :ref:`PackedVector3Array<class_PackedVector3Array>` from )
Constructs an array from a :ref:`PackedVector3Array<class_PackedVector3Array>`.
.. rst-class:: classref-section-separator
.. rst-class:: classref-descriptions-group
.. rst-class:: classref-method
:ref:`bool<class_bool>` all ( :ref:`Callable<class_Callable>` method ) |const|
Calls the provided :ref:`Callable<class_Callable>` on each element in the array and returns true
if the :ref:`Callable<class_Callable>` returns true
for all elements in the array. If the :ref:`Callable<class_Callable>` returns false
for one array element or more, this method returns false
.
The callable's method should take one :ref:`Variant<class_Variant>` parameter (the current array element) and return a boolean value.
func _ready(): print([6, 10, 6].all(greater_than_5)) # Prints True (3/3 elements evaluate to `true`). print([4, 10, 4].all(greater_than_5)) # Prints False (1/3 elements evaluate to `true`). print([4, 4, 4].all(greater_than_5)) # Prints False (0/3 elements evaluate to `true`). print([].all(greater_than_5)) # Prints True (0/0 elements evaluate to `true`). print([6, 10, 6].all(func(number): return number > 5)) # Prints True. Same as the first line above, but using lambda function. func greater_than_5(number): return number > 5
See also :ref:`any<class_Array_method_any>`, :ref:`filter<class_Array_method_filter>`, :ref:`map<class_Array_method_map>` and :ref:`reduce<class_Array_method_reduce>`.
Note: Unlike relying on the size of an array returned by :ref:`filter<class_Array_method_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
.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`bool<class_bool>` any ( :ref:`Callable<class_Callable>` method ) |const|
Calls the provided :ref:`Callable<class_Callable>` on each element in the array and returns true
if the :ref:`Callable<class_Callable>` returns true
for one or more elements in the array. If the :ref:`Callable<class_Callable>` returns false
for all elements in the array, this method returns false
.
The callable's method should take one :ref:`Variant<class_Variant>` parameter (the current array element) and return a boolean value.
func _ready(): print([6, 10, 6].any(greater_than_5)) # Prints True (3 elements evaluate to `true`). print([4, 10, 4].any(greater_than_5)) # Prints True (1 elements evaluate to `true`). print([4, 4, 4].any(greater_than_5)) # Prints False (0 elements evaluate to `true`). print([].any(greater_than_5)) # Prints False (0 elements evaluate to `true`). print([6, 10, 6].any(func(number): return number > 5)) # Prints True. Same as the first line above, but using lambda function. func greater_than_5(number): return number > 5
See also :ref:`all<class_Array_method_all>`, :ref:`filter<class_Array_method_filter>`, :ref:`map<class_Array_method_map>` and :ref:`reduce<class_Array_method_reduce>`.
Note: Unlike relying on the size of an array returned by :ref:`filter<class_Array_method_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
.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void append ( :ref:`Variant<class_Variant>` value )
Appends an element at the end of the array (alias of :ref:`push_back<class_Array_method_push_back>`).
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void append_array ( :ref:`Array<class_Array>` array )
Appends another array at the end of this array.
var array1 = [1, 2, 3] var array2 = [4, 5, 6] array1.append_array(array2) print(array1) # Prints [1, 2, 3, 4, 5, 6].
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void assign ( :ref:`Array<class_Array>` array )
Assigns elements of another array
into the array. Resizes the array to match array
. Performs type conversions if the array is typed.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` back ( ) |const|
Returns the last element of the array. Prints an error and returns null
if the array is empty.
Note: Calling this function is not the same as writing array[-1]
. If the array is empty, accessing by index will pause project execution when running from the editor.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`int<class_int>` bsearch ( :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` before=true ) |const|
Finds the index of an existing value (or the insertion index that maintains sorting order, if the value is not yet present in the array) using binary search. Optionally, a before
specifier can be passed. If false
, the returned index comes after all existing entries of the value in the array.
Note: Calling :ref:`bsearch<class_Array_method_bsearch>` on an unsorted array results in unexpected behavior.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`int<class_int>` bsearch_custom ( :ref:`Variant<class_Variant>` value, :ref:`Callable<class_Callable>` func, :ref:`bool<class_bool>` before=true ) |const|
Finds the index of an existing value (or the insertion index that maintains sorting order, if the value is not yet present in the array) using binary search and a custom comparison method. Optionally, a before
specifier can be passed. If false
, the returned index comes after all existing entries of the value in the array. The custom method receives two arguments (an element from the array and the value searched for) and must return true
if the first argument is less than the second, and return false
otherwise.
Note: Calling :ref:`bsearch_custom<class_Array_method_bsearch_custom>` on an unsorted array results in unexpected behavior.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void clear ( )
Clears the array. This is equivalent to using :ref:`resize<class_Array_method_resize>` with a size of 0
.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`int<class_int>` count ( :ref:`Variant<class_Variant>` value ) |const|
Returns the number of times an element is in the array.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Array<class_Array>` duplicate ( :ref:`bool<class_bool>` deep=false ) |const|
Returns a copy of the array.
If deep
is true
, a deep copy is performed: all nested arrays and dictionaries are duplicated and will not be shared with the original array. If false
, a shallow copy is made and references to the original nested arrays and dictionaries are kept, so that modifying a sub-array or dictionary in the copy will also impact those referenced in the source array. Note that any :ref:`Object<class_Object>`-derived elements will be shallow copied regardless of the deep
setting.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void erase ( :ref:`Variant<class_Variant>` value )
Removes the first occurrence of a value from the array. If the value does not exist in the array, nothing happens. To remove an element by index, use :ref:`remove_at<class_Array_method_remove_at>` instead.
Note: This method acts in-place and doesn't return a value.
Note: On large arrays, this method will be slower if the removed element is close to the beginning of the array (index 0). This is because all elements placed after the removed element have to be reindexed.
Note: Do not erase entries while iterating over the array.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void fill ( :ref:`Variant<class_Variant>` value )
Assigns the given value to all elements in the array. This can typically be used together with :ref:`resize<class_Array_method_resize>` to create an array with a given size and initialized elements:
.. tabs:: .. code-tab:: gdscript var array = [] array.resize(10) array.fill(0) # Initialize the 10 elements to 0. .. code-tab:: csharp var array = new Godot.Collections.Array(); array.Resize(10); array.Fill(0); // Initialize the 10 elements to 0.
Note: If value
is of a reference type (:ref:`Object<class_Object>`-derived, Array, :ref:`Dictionary<class_Dictionary>`, etc.) then the array is filled with the references to the same object, i.e. no duplicates are created.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Array<class_Array>` filter ( :ref:`Callable<class_Callable>` method ) |const|
Calls the provided :ref:`Callable<class_Callable>` on each element in the array and returns a new array with the elements for which the method returned true
.
The callable's method should take one :ref:`Variant<class_Variant>` parameter (the current array element) and return a boolean value.
func _ready(): print([1, 2, 3].filter(remove_1)) # Prints [2, 3]. print([1, 2, 3].filter(func(number): return number != 1)) # Same as above, but using lambda function. func remove_1(number): return number != 1
See also :ref:`any<class_Array_method_any>`, :ref:`all<class_Array_method_all>`, :ref:`map<class_Array_method_map>` and :ref:`reduce<class_Array_method_reduce>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`int<class_int>` find ( :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=0 ) |const|
Searches the array for a value and returns its index or -1
if not found. Optionally, the initial search index can be passed.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` front ( ) |const|
Returns the first element of the array. Prints an error and returns null
if the array is empty.
Note: Calling this function is not the same as writing array[0]
. If the array is empty, accessing by index will pause project execution when running from the editor.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`int<class_int>` get_typed_builtin ( ) |const|
Returns the :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>` constant for a typed array. If the Array is not typed, returns :ref:`@GlobalScope.TYPE_NIL<class_@GlobalScope_constant_TYPE_NIL>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`StringName<class_StringName>` get_typed_class_name ( ) |const|
Returns a class name of a typed Array of type :ref:`@GlobalScope.TYPE_OBJECT<class_@GlobalScope_constant_TYPE_OBJECT>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` get_typed_script ( ) |const|
Returns the script associated with a typed array tied to a class name.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`bool<class_bool>` has ( :ref:`Variant<class_Variant>` value ) |const|
Returns true
if the array contains the given value.
.. tabs:: .. code-tab:: gdscript print(["inside", 7].has("inside")) # True print(["inside", 7].has("outside")) # False print(["inside", 7].has(7)) # True print(["inside", 7].has("7")) # False .. code-tab:: csharp var arr = new Godot.Collections.Array { "inside", 7 }; // has is renamed to Contains GD.Print(arr.Contains("inside")); // True GD.Print(arr.Contains("outside")); // False GD.Print(arr.Contains(7)); // True GD.Print(arr.Contains("7")); // False
Note: This is equivalent to using the in
operator as follows:
.. tabs:: .. code-tab:: gdscript # Will evaluate to `true`. if 2 in [2, 4, 6, 8]: print("Contains!") .. code-tab:: csharp // As there is no "in" keyword in C#, you have to use Contains var array = new Godot.Collections.Array { 2, 4, 6, 8 }; if (array.Contains(2)) { GD.Print("Contains!"); }
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`int<class_int>` hash ( ) |const|
Returns a hashed 32-bit integer value representing the array and its contents.
Note: Arrays with equal content will always produce identical hash values. However, the reverse is not true. Returning identical hash values does not imply the arrays are equal, because different arrays can have identical hash values due to hash collisions.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`int<class_int>` insert ( :ref:`int<class_int>` position, :ref:`Variant<class_Variant>` value )
Inserts a new element at a given position in the array. The position must be valid, or at the end of the array (pos == size()
).
Note: This method acts in-place and doesn't return a value.
Note: On large arrays, this method will be slower if the inserted element is close to the beginning of the array (index 0). This is because all elements placed after the newly inserted element have to be reindexed.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`bool<class_bool>` is_empty ( ) |const|
Returns true
if the array is empty.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`bool<class_bool>` is_read_only ( ) |const|
Returns true
if the array is read-only. See :ref:`make_read_only<class_Array_method_make_read_only>`. Arrays are automatically read-only if declared with const
keyword.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`bool<class_bool>` is_same_typed ( :ref:`Array<class_Array>` array ) |const|
Returns true
if the array is typed the same as array
.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`bool<class_bool>` is_typed ( ) |const|
Returns true
if the array is typed. Typed arrays can only store elements of their associated type and provide type safety for the []
operator. Methods of typed array still return :ref:`Variant<class_Variant>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void make_read_only ( )
Makes the array read-only, i.e. disabled modifying of the array's elements. Does not apply to nested content, e.g. content of nested arrays.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Array<class_Array>` map ( :ref:`Callable<class_Callable>` method ) |const|
Calls the provided :ref:`Callable<class_Callable>` for each element in the array and returns a new array filled with values returned by the method.
The callable's method should take one :ref:`Variant<class_Variant>` parameter (the current array element) and can return any :ref:`Variant<class_Variant>`.
func _ready(): print([1, 2, 3].map(negate)) # Prints [-1, -2, -3]. print([1, 2, 3].map(func(number): return -number)) # Same as above, but using lambda function. func negate(number): return -number
See also :ref:`filter<class_Array_method_filter>`, :ref:`reduce<class_Array_method_reduce>`, :ref:`any<class_Array_method_any>` and :ref:`all<class_Array_method_all>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` max ( ) |const|
Returns the maximum value contained in the array if all elements are of comparable types. If the elements can't be compared, null
is returned.
To find the maximum value using a custom comparator, you can use :ref:`reduce<class_Array_method_reduce>`. In this example every array element is checked and the first maximum value is returned:
func _ready(): var arr = [Vector2(0, 1), Vector2(2, 0), Vector2(1, 1), Vector2(1, 0), Vector2(0, 2)] # In this example we compare the lengths. print(arr.reduce(func(max, val): return val if is_length_greater(val, max) else max)) func is_length_greater(a, b): return a.length() > b.length()
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` min ( ) |const|
Returns the minimum value contained in the array if all elements are of comparable types. If the elements can't be compared, null
is returned.
See also :ref:`max<class_Array_method_max>` for an example of using a custom comparator.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` pick_random ( ) |const|
Returns a random value from the target array.
.. tabs:: .. code-tab:: gdscript var array: Array[int] = [1, 2, 3, 4] print(array.pick_random()) # Prints either of the four numbers. .. code-tab:: csharp var array = new Godot.Collections.Array { 1, 2, 3, 4 }; GD.Print(array.PickRandom()); // Prints either of the four numbers.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` pop_at ( :ref:`int<class_int>` position )
Removes and returns the element of the array at index position
. If negative, position
is considered relative to the end of the array. Leaves the array untouched and returns null
if the array is empty or if it's accessed out of bounds. An error message is printed when the array is accessed out of bounds, but not when the array is empty.
Note: On large arrays, this method can be slower than :ref:`pop_back<class_Array_method_pop_back>` as it will reindex the array's elements that are located after the removed element. The larger the array and the lower the index of the removed element, the slower :ref:`pop_at<class_Array_method_pop_at>` will be.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` pop_back ( )
Removes and returns the last element of the array. Returns null
if the array is empty, without printing an error message. See also :ref:`pop_front<class_Array_method_pop_front>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` pop_front ( )
Removes and returns the first element of the array. Returns null
if the array is empty, without printing an error message. See also :ref:`pop_back<class_Array_method_pop_back>`.
Note: On large arrays, this method is much slower than :ref:`pop_back<class_Array_method_pop_back>` as it will reindex all the array's elements every time it's called. The larger the array, the slower :ref:`pop_front<class_Array_method_pop_front>` will be.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void push_back ( :ref:`Variant<class_Variant>` value )
Appends an element at the end of the array. See also :ref:`push_front<class_Array_method_push_front>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void push_front ( :ref:`Variant<class_Variant>` value )
Adds an element at the beginning of the array. See also :ref:`push_back<class_Array_method_push_back>`.
Note: On large arrays, this method is much slower than :ref:`push_back<class_Array_method_push_back>` as it will reindex all the array's elements every time it's called. The larger the array, the slower :ref:`push_front<class_Array_method_push_front>` will be.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` reduce ( :ref:`Callable<class_Callable>` method, :ref:`Variant<class_Variant>` accum=null ) |const|
Calls the provided :ref:`Callable<class_Callable>` for each element in array and accumulates the result in accum
.
The callable's method takes two arguments: the current value of accum
and the current array element. If accum
is null
(default value), the iteration will start from the second element, with the first one used as initial value of accum
.
func _ready(): print([1, 2, 3].reduce(sum, 10)) # Prints 16. print([1, 2, 3].reduce(func(accum, number): return accum + number, 10)) # Same as above, but using lambda function. func sum(accum, number): return accum + number
See also :ref:`map<class_Array_method_map>`, :ref:`filter<class_Array_method_filter>`, :ref:`any<class_Array_method_any>` and :ref:`all<class_Array_method_all>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void remove_at ( :ref:`int<class_int>` position )
Removes an element from the array by index. If the index does not exist in the array, nothing happens. To remove an element by searching for its value, use :ref:`erase<class_Array_method_erase>` instead.
Note: This method acts in-place and doesn't return a value.
Note: On large arrays, this method will be slower if the removed element is close to the beginning of the array (index 0). This is because all elements placed after the removed element have to be reindexed.
Note: 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 without returning the value, use arr.resize(arr.size() - 1)
.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`int<class_int>` resize ( :ref:`int<class_int>` size )
Resizes the array to contain a different number of elements. If the array size is smaller, elements are cleared, if bigger, new elements are null
.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void reverse ( )
Reverses the order of the elements in the array.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`int<class_int>` rfind ( :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=-1 ) |const|
Searches the array in reverse order. Optionally, a start search index can be passed. If negative, the start index is considered relative to the end of the array.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void shuffle ( )
Shuffles the array such that the items will have a random order. This method uses the global random number generator common to methods such as :ref:`@GlobalScope.randi<class_@GlobalScope_method_randi>`. Call :ref:`@GlobalScope.randomize<class_@GlobalScope_method_randomize>` to ensure that a new seed will be used each time if you want non-reproducible shuffling.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`int<class_int>` size ( ) |const|
Returns the number of elements in the array.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Array<class_Array>` slice ( :ref:`int<class_int>` begin, :ref:`int<class_int>` end=2147483647, :ref:`int<class_int>` step=1, :ref:`bool<class_bool>` deep=false ) |const|
Returns the slice of the Array, from begin
(inclusive) to end
(exclusive), as a new Array.
The absolute value of begin
and end
will be clamped to the array size, so the default value for end
makes it slice to the size of the array by default (i.e. arr.slice(1)
is a shorthand for arr.slice(1, arr.size())
).
If either begin
or end
are negative, they will be relative to the end of the array (i.e. arr.slice(0, -2)
is a shorthand for arr.slice(0, arr.size() - 2)
).
If specified, step
is the relative index between source elements. It can be negative, then begin
must be higher than end
. For example, [0, 1, 2, 3, 4, 5].slice(5, 1, -2)
returns [5, 3]
.
If deep
is true, each element will be copied by value rather than by reference.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void sort ( )
Sorts the array.
Note: The sorting algorithm used is not stable. This means that values considered equal may have their order changed when using :ref:`sort<class_Array_method_sort>`.
Note: Strings are sorted in alphabetical order (as opposed to natural order). This may lead to unexpected behavior when sorting an array of strings ending with a sequence of numbers. Consider the following example:
.. tabs:: .. code-tab:: gdscript var strings = ["string1", "string2", "string10", "string11"] strings.sort() print(strings) # Prints [string1, string10, string11, string2] .. code-tab:: csharp var strings = new Godot.Collections.Array { "string1", "string2", "string10", "string11" }; strings.Sort(); GD.Print(strings); // Prints [string1, string10, string11, string2]
To perform natural order sorting, you can use :ref:`sort_custom<class_Array_method_sort_custom>` with :ref:`String.naturalnocasecmp_to<class_String_method_naturalnocasecmp_to>` as follows:
var strings = ["string1", "string2", "string10", "string11"] strings.sort_custom(func(a, b): return a.naturalnocasecmp_to(b) < 0) print(strings) # Prints [string1, string2, string10, string11]
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
void sort_custom ( :ref:`Callable<class_Callable>` func )
Sorts the array using a custom method. The custom method receives two arguments (a pair of elements from the array) and must return either true
or false
. For two elements a
and b
, if the given method returns true
, element b
will be after element a
in the array.
Note: The sorting algorithm used is not stable. This means that values considered equal may have their order changed when using :ref:`sort_custom<class_Array_method_sort_custom>`.
Note: You cannot randomize the return value as the heapsort algorithm expects a deterministic result. Randomizing the return value will result in unexpected behavior.
.. tabs:: .. code-tab:: gdscript func sort_ascending(a, b): if a[0] < b[0]: return true return false func _ready(): var my_items = [[5, "Potato"], [9, "Rice"], [4, "Tomato"]] my_items.sort_custom(sort_ascending) print(my_items) # Prints [[4, Tomato], [5, Potato], [9, Rice]]. # Descending, lambda version. my_items.sort_custom(func(a, b): return a[0] > b[0]) print(my_items) # Prints [[9, Rice], [5, Potato], [4, Tomato]]. .. code-tab:: csharp // There is no custom sort support for Godot.Collections.Array
.. rst-class:: classref-section-separator
.. rst-class:: classref-descriptions-group
.. rst-class:: classref-operator
:ref:`bool<class_bool>` operator != ( :ref:`Array<class_Array>` right )
Compares the left operand Array against the right
Array. Returns true
if the sizes or contents of the arrays are not equal, false
otherwise.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`Array<class_Array>` operator + ( :ref:`Array<class_Array>` right )
Concatenates two Arrays together, with the right
Array being added to the end of the Array specified in the left operand. For example, [1, 2] + [3, 4]
results in [1, 2, 3, 4]
.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`bool<class_bool>` operator < ( :ref:`Array<class_Array>` right )
Performs a comparison for each index between the left operand Array and the right
Array, considering the highest common index of both arrays for this comparison: Returns true
on the first occurrence of an element that is less, or false
if the element is greater. Note that depending on the type of data stored, this function may be recursive. If all elements are equal, it compares the length of both arrays and returns false
if the left operand Array has fewer elements, otherwise it returns true
.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`bool<class_bool>` operator <= ( :ref:`Array<class_Array>` right )
Performs a comparison for each index between the left operand Array and the right
Array, considering the highest common index of both arrays for this comparison: Returns true
on the first occurrence of an element that is less, or false
if the element is greater. Note that depending on the type of data stored, this function may be recursive. If all elements are equal, it compares the length of both arrays and returns true
if the left operand Array has the same number of elements or fewer, otherwise it returns false
.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`bool<class_bool>` operator == ( :ref:`Array<class_Array>` right )
Compares the left operand Array against the right
Array. Returns true
if the sizes and contents of the arrays are equal, false
otherwise.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`bool<class_bool>` operator > ( :ref:`Array<class_Array>` right )
Performs a comparison for each index between the left operand Array and the right
Array, considering the highest common index of both arrays for this comparison: Returns true
on the first occurrence of an element that is greater, or false
if the element is less. Note that depending on the type of data stored, this function may be recursive. If all elements are equal, it compares the length of both arrays and returns true
if the right
Array has more elements, otherwise it returns false
.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`bool<class_bool>` operator >= ( :ref:`Array<class_Array>` right )
Performs a comparison for each index between the left operand Array and the right
Array, considering the highest common index of both arrays for this comparison: Returns true
on the first occurrence of an element that is greater, or false
if the element is less. Note that depending on the type of data stored, this function may be recursive. If all elements are equal, it compares the length of both arrays and returns true
if the right
Array has more or the same number of elements, otherwise it returns false
.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`Variant<class_Variant>` operator [] ( :ref:`int<class_int>` index )
Returns a reference to the element of type :ref:`Variant<class_Variant>` at the specified location. Arrays start at index 0. index
can be a zero or positive value to start from the beginning, or a negative value to start from the end. Out-of-bounds array access causes a run-time error, which will result in an error being printed and the project execution pausing if run from the editor.