NTGML API: Arrays

array_length_1d(val)​

Same thing as array_length, here for backwards compatibility.

Prefixed functions

Here you may observe an implementation detail:

When I was making the scripting API for NTT, GameMaker did not have many built-in functions for array manipulation and I implemented a handful of these myself.

Some years later GM added a good handful of array functions, but some of these have different signatures from how my functions, so they now have a native_ prefix.

NTT-specific functions use the built-in functions where appropriate so there is no speed penalty from using one or the other.

NTT-specific functions
NTT-specific functions
array_clear(array, value)

Replaces the contents of an array with the given value.

var arr = [1, 2, 3];
array_clear(arr, 4);
trace(arr); // [4, 4, 4]
array_clone(array)​

Returns a shallow copy of the given array.

var a = [1, 2, 3];
var b = array_clone(a);
a[1] = 4;
trace(a); // [1, 4, 3]
trace(b); // [1, 2, 3]
array_slice(array, start, length)​

Returns a new array with values representing a section of an array.

This is more or less a shortcut for array_create(length) + array_copy.

var arr = [1, 2, 3, 4, 5];
trace(array_slice(arr, 1, 3)); // [1, 2, 3]

array_find_index(array, value)​

Returns the index of first occurrence of a value in the array.

var arr = ["a", "b", "c"];
trace(array_find_index(arr, "b")); // 1
trace(array_find_index(arr, "c")); // 2
trace(array_find_index(arr, "d")); // -1

This is the primary reason for the native_ prefixes - in GML functions, array_find_index uses a predicate to search an array while array_get_index searches for a value. A confusing one given the names of ds_list functions.

array_find_index_ext(array, value, start)​

Like above, but starts searching at a specific index (inclusive).

array_find_last_index(array, val)​

Like above, but searches from the end of an array.

array_find_last_index_ext(array, val, start)​

Like above, but searches for values before the specified index (exclusive).


array_filter(array, fn, copy=true)​

Filters an array by passing each item to the filter function and keeping only the ones that it returned true for.

If copy is true, filtered items will be added to a new array.
Otherwise the array is modified in place.

Returns the array (either new or the input one) in both cases.

On terms of modern GML, the function is a mix of array_filter and array_filter_ext.

array_map(array, fn, copy=true)​

Modifies an array by passing all items through a mapper function.

If copy is true, resulting items will be added to a new array.
Otherwise the array is modified in place.

Returns the array (either new or the input one) in both cases.

On terms of modern GML, the function is a mix of array_map and array_map_ext.


array_join(array, delimiter:string)

Joins the contents of an array into a string, separating them as specified.

var arr = [1, 2, 3];
trace(array_join(arr, "|")); // "1|2|3"

string_split can be considered a counterpart of this function.


array_sort_sub(array, sub_index, ascend)​

Assuming an array of arrays, sorts the contents based on sub_index-th element of each sub-array.

These days you can pass a predicate function to array_sort.