array_copy

This function copies all or part of an array into another array at any position.

You supply both the source and the destination arrays (both created previously), as well as a position within the source array to copy from and a position within the destination array to copy to. Finally you need to specify the length of the array (or the length of the part that you want) to copy. If the data being copied exceeds the length of the destination array, the array will be extended to accept the data. The function supports specifying negative values for the index and number of elements, as described under Offset And Length.

This function can also be used for multidimensional arrays, as long as you specify which dimension you want to copy when you supply the array index, following this pattern:

// Copy to the first dimension of an array
// from the second dimension of an array
array_copy(item_array, 0, inventory_array[0], 0, len);

// Copy to the third dimension of an array
// from the first dimension of an array
array_copy(item_array[0][0], 0, inventory_array, 0, len);

// etc...

 

Syntax:

array_copy(dest, dest_index, src, src_index, length);

ArgumentTypeDescription
destArrayThe reference to the array to copy to.
dest_indexRealThe index within the array to copy to. Negative indices are supported and count from the end of the array. An offset of -1 refers to the last element of the array, an offset of -2 to the one before last element, etc. (see Offset And Length)
srcArrayThe reference to the array to copy from.
src_indexRealThe index within the array to start copying from. Negative indices are supported and count from the end of the array. An offset of -1 refers to the last element of the array, an offset of -2 to the one before last element, etc. (see Offset And Length)
lengthRealThe length (number of array indices) to copy, starting with the value at src_index. A negative value can be provided for length; in this case copying also starts at index src_index, but the next elements that are copied are src_index-1, src_index-2, etc. I.e. elements are copied "backwards". Note that the first value is always written at dest_index, the second value at dest_index+1, etc.

 

Returns:

N/A

 

Example 1: Basic Use

if (!array_equals(inventory_array, item_array))
{
    var _len = array_length(inventory_array);
    array_copy(item_array, 0, inventory_array, 0, _len);
}

The above code checks two arrays using array_equals to see if they hold equivalent values. If they don't, the code will copy the entire contents of the array inventory_array into the array item_array.

 

Example 2: Negative Length

var _a = [1, 2, 3, 4];
var _b = [5, 6, 7, 8];

array_copy(_a, 1, _b, -1, -2);
show_debug_message(_a);

In the above example, first two temporary arrays _a and _b are initialised. Next two elements are copied from _b to _a. The src_index and length parameter are set to -1 and -2, respectively. This means that the last (-1) and the one before last array element are copied (2 elements, counting backwards because of the minus sign, from the end of array _b). This means the values of the last element and one before last element of _b are copied to positions 1 (the value of the dest_index parameter) and 2 in _a respectively.

A debug message shows the contents of array _a after copying.