Skip to main content

Array

Description:

  An array data structure that supports various operations. The Array record is designed to be 1-based indexing, which means that the first item in the array has an index of 1. This is the same behavior of Lua table used as an array.

Class Object: Array Class.

Inherits from: Object.

count

Type: Readonly Field.

Description:

  The number of items in the array.

Signature:

const count: integer

empty

Type: Readonly Field.

Description:

  Whether the array is empty or not.

Signature:

const empty: boolean

addRange

Type: Function.

Description:

  Adds all items from another array to the end of this array.

Signature:

addRange: function(self: Array, other: Array)

Parameters:

ParameterTypeDescription
otherArrayAnother array object.

removeFrom

Type: Function.

Description:

  Removes all items from this array that are also in another array.

Signature:

removeFrom: function(self: Array, other: Array)

Parameters:

ParameterTypeDescription
otherArrayAnother array object.

clear

Type: Function.

Description:

  Removes all items from the array.

Signature:

clear: function(self: Array)

reverse

Type: Function.

Description:

  Reverses the order of the items in the array.

Signature:

reverse: function(self: Array)

shrink

Type: Function.

Description:

  Removes any empty slots from the end of the array. Used for release the unused memory this array holds.

Signature:

shrink: function(self: Array)

swap

Type: Function.

Description:

  Swaps the items at two given indices.

Signature:

swap: function(self: Array, indexA: integer, indexB: integer)

Parameters:

ParameterTypeDescription
indexAintegerThe first index.
indexBintegerThe second index.

removeAt

Type: Function.

Description:

  Removes the item at the given index.

Signature:

removeAt: function(self: Array, index: integer): boolean

Parameters:

ParameterTypeDescription
indexintegerThe index to remove.

Returns:

Return TypeDescription
booleanTrue if an item was removed, false otherwise.

fastRemoveAt

Type: Function.

Description:

  Removes the item at the given index without preserving the order of the array.

Signature:

fastRemoveAt: function(self: Array, index: integer): boolean

Parameters:

ParameterTypeDescription
indexintegerThe index to remove.

Returns:

Return TypeDescription
booleanTrue if an item was removed, false otherwise.

each

Type: Function.

Description:

  Calls a given function for each item in the array.

Signature:

each: function(self: Array, func: function(Object): boolean): boolean

Parameters:

ParameterTypeDescription
funcfunctionThe function to call for each item.Should return false to continue iteration, true to stop.

Returns:

Return TypeDescription
booleanFalse if the iteration completed, true if it was interrupted by the function.

first

Type: Readonly Field.

Description:

  The first item in the array.

Signature:

const first: Item

last

Type: Readonly Field.

Description:

  The last item in the array.

Signature:

const last: Item

randomObject

Type: Readonly Field.

Description:

  A random item from the array.

Signature:

const randomObject: Item

set

Type: Function.

Description:

  Sets the item at the given index.

Signature:

set: function(self: Array, index: integer, item: Item)

Parameters:

ParameterTypeDescription
indexintegerThe index to set, should be 1 based.
itemItemThe new item value.

get

Type: Function.

Description:

  Gets the item at the given index.

Signature:

get: function(self: Array, index: integer): Item

Parameters:

ParameterTypeDescription
indexintegerThe index to get, should be 1 based.

Returns:

Return TypeDescription
ItemThe item value.

add

Type: Function.

Description:

  Adds an item to the end of the array.

Signature:

add: function(self: Array, item: Item)

Parameters:

ParameterTypeDescription
itemItemThe item to add.

insert

Type: Function.

Description:

  Inserts an item at the given index, shifting other items to the right.

Signature:

insert: function(self: Array, index: integer, item: Item)

Parameters:

ParameterTypeDescription
indexintegerThe index to insert at.
itemItemThe item to insert.

contains

Type: Function.

Description:

  Checks whether the array contains a given item.

Signature:

contains: function(self: Array, item: Item): boolean

Parameters:

ParameterTypeDescription
itemItemThe item to check.

Returns:

Return TypeDescription
booleanTrue if the item is found, false otherwise.

index

Type: Function.

Description:

  Gets the index of a given item.

Signature:

index: function(self: Array, item: Item): integer

Parameters:

ParameterTypeDescription
itemItemThe item to search for.

Returns:

Return TypeDescription
integerThe index of the item, or 0 if it is not found.

removeLast

Type: Function.

Description:

  Removes and returns the last item in the array.

Signature:

removeLast: function(self: Array): Item

Returns:

Return TypeDescription
ItemThe last item removed from the array.

fastRemove

Type: Function.

Description:

  Removes the first occurrence of a given item from the array without preserving order.

Signature:

fastRemove: function(self: Array, item: Item): boolean

Parameters:

ParameterTypeDescription
itemItemThe item to remove.

Returns:

Return TypeDescription
booleanTrue if the item was found and removed, false otherwise.

__index

Type: Metamethod.

Description:

  Metamethod to access the item at the given index using the [] operator.

Signature:

metamethod __index: function(self: Array, index: integer): Item

Parameters:

ParameterTypeDescription
indexintegerThe index to get, should be 1 based.

Returns:

Return TypeDescription
ItemThe item value.

__newindex

Type: Metamethod.

Description:

  Metamethod to set the item at the given index using the [] operator.

Signature:

metamethod __newindex: function(self: Array, index: integer, item: Item)

Parameters:

ParameterTypeDescription
indexintegerThe index to set, should be 1 based.
itemItemThe new item value.

__len

Type: Metamethod.

Description:

  Metamethod to get the length of the array using the # operator.

Signature:

metamethod __len: function(self: Array): integer

Returns:

Return TypeDescription
integerThe length of the array.