AngelScript
Arrays

Observe: Arrays are only available in the scripts if the application registers the support for them. The syntax for using arrays may differ for the application you're working with so consult the application's manual for more details.

See Also
array template object

It is possible to declare array variables with the array identifier followed by the type of the elements within angle brackets.

Example:

  array<int> a, b, c;
  array<Foo@> d;

a, b, and c are now arrays of integers, and d is an array of handles to objects of the Foo type.

When declaring arrays it is possible to define the initial size of the array by passing the length as a parameter to the constructor. The elements can also be individually initialized by specifying an initialization list. Example:

  array<int> a;           // A zero-length array of integers
  array<int> b(3);        // An array of integers with 3 elements
  array<int> c(3, 1);     // An array of integers with 3 elements, all set to 1 by default
  array<int> d = {,3,4,}; // An array of integers with 4 elements, where
                          // the second and third elements are initialized

Multidimensional arrays are supported as arrays of arrays, for example:

  array<array<int>> a;                     // An empty array of arrays of integers
  array<array<int>> b = {{1,2},{3,4}}      // A 2 by 2 array with initialized values
  array<array<int>> c(10, array<int>(10)); // A 10 by 10 array of integers with uninitialized values

Each element in the array is accessed with the indexing operator. The indices are zero based, i.e. the range of valid indices are from 0 to length - 1.

  a[0] = some_value;

When the array stores handles the elements are assigned using the handle assignment.

  // Declare an array with initial length 1
  array<Foo@> arr(1);
  // Set the first element to point to a new instance of Foo
  @arr[0] = Foo();

Supporting array object and functions

The array object supports a number of operators and has several class methods to facilitate the manipulation of strings.

The array object is a reference type even if the elements are not, so it's possible to use handles to the array object when passing it around to avoid costly copies.

Operators

  • = assignment
  • [] index operator
  • ==, != equality

Methods

  • uint length() const
  • void resize(uint)
  • void reverse()
  • void insertAt(uint index, const T& in)
  • void insertLast(const T& in)
  • void removeAt(uint index)
  • void removeLast()
  • void sortAsc()
  • void sortAsc(uint startAt, uint count)
  • void sortDesc()
  • void sortDesc(uint startAt, uint count)
  • int find(const T& in)
  • int find(uint startAt, const T& in)
  • int findByRef(const T& in)
  • int findByRef(uint startAt, const T& in)

The T represents the type of the array elements.

Script example:

  int main()
  {
    array<int> arr = {1,2,3}; // 1,2,3
    arr.insertLast(0);        // 1,2,3,0
    arr.insertAt(2,4);        // 1,2,4,3,0
    arr.removeAt(1);          // 1,4,3,0
    arr.sortAsc();            // 0,1,3,4
    int sum = 0;
    for( uint n = 0; n < arr.length(); n++ )
      sum += arr[n];
    return sum;
  }