Array

    Generic array datatype.

    Generic array which can contain several elements of any type, accessible by a numerical index starting at 0. Negative indices can be used to count from the back, like in Python (-1 is the last element, -2 the second to last, etc.). Example:

    Arrays are always passed by reference.

    Constructs an array from a .


    Constructs an array from a PoolVector3Array.


    Constructs an array from a .


    Constructs an array from a PoolStringArray.


    Constructs an array from a .


    Constructs an array from a PoolIntArray.


    Constructs an array from a .


    Appends an element at the end of the array (alias of ).


    Returns the last element of the array if the array is not empty.


    • int bsearch ( value, bool before=True )

    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 , the returned index comes after all existing entries of the value in the array. Note that calling bsearch on an unsorted array results in unexpected behavior.



    • void clear ( )

    Clears the array (resizes to 0).


    • int count ( value )

    Returns the number of times an element is in the array.


    • Array duplicate ( deep=False )

    Returns a copy of the array.

    If 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.


    Returns true if the array is empty.


    • void erase ( value )

    Removes the first occurrence of a value from the array.


    • int find ( what, int from=0 )

    Searches the array for a value and returns its index or -1 if not found. Optionally, the initial search index can be passed.


    Searches the array in reverse order for a value and returns its index or -1 if not found.


    • front ( )

    Returns the first element of the array if the array is not empty.


    Returns if the array contains the given value.


    Returns a hashed integer value representing the array contents.


    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()).


    • void invert ( )

    Reverses the order of the elements in the array.


    • max ( )

    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.


    • pop_back ( )

    Removes the last element of the array.


    Removes the first element of the array.


    • void push_back ( value )

    Appends an element at the end of the array.


    Adds an element at the beginning of the array.


    • void remove ( position )

    Removes an element from the array by index.


    • void resize ( 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.


    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.


    • 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 @GDScript.randi. Call to ensure that a new seed will be used each time if you want non-reproducible shuffling.


    Returns the number of elements in the array.


    Sorts the array. Note: strings are sorted in alphabetical, not natural order.


    • void sort_custom ( obj, String func )

    Sorts the array using a custom method. The arguments are an object that holds the method and the name of such method. The custom method receives two arguments (a pair of elements from the array) and must return if the first argument is less than the second, and return false otherwise.

    Note: you cannot randomize the return value as the heapsort algorithm expects a deterministic result. Doing so will result in unexpected behavior.