Patron Wiki
Advertisement

An array is a collection of variables of the same type. Meaning, you can have an array of integers, but not an array of integers and floats. A great usage example of arrays is to receive a collection of all resources that the player currently has above a certain number in stock. Then you can do something with them, perhaps choose one of them for the King to request ;).

Working with arrays[]

array int arrayName ;

Before using an array, you must initialize it. This means, you have to know what type of variable the array will hold. Thankfully, you don't have to know how many items it will have in advance. In this regard, arrays in SAL are more similar to Lists in for example C# than arrays.

array Add arrayName 10 ;

The above is the simplest way of manually adding a single value to an array. You can, of course, do this multiple times, one per line, but if you want to add multiple values to the array, you can also do this at the same time.

array Add arrayName 10 15 5 18 ;

Just don't go overboard with this. There's a limit to how many values you can add in the same function call. Don't go above 10, just to be on the safe side.

Array are indexed. This means that you can retrieve a value from it by referencing it's array index. Keep in mind that, for arrays, indexing starts at 0, NOT 1. The FIRST "slot" in the array has the index of 0.

Common array functions[]

Get

/* get the value of an item in the array, by index */
int index = 0 ;
float returnValue = 0.0 ;
array float arrayOfFloats ;
array arrayOfFloats returnValue index ;

GetElementNum

/* get the current number of items in the array */
int numberOfItems = 0 ;
array float arrayOfFloats ;
array GetElementNum arrayOfFloats numberOfItems ;

Add

/* add item(s) to array */
array float arrayOfFloats ;
array Add arrayOfFloats 5.1 2.8 101.0 ;

RemoveVal

/* find all the instances of the valueToRemove in the array and remove them from the array */
array int arrayOfInts ;
int valueToRemove = 5 ;
array RemoveVal arrayOfInts valueToRemove ;

RemoveAll

/* remove all values from the array; "clear" array */
array int arrayOfInts ;
array RemoveAll arrayOfInts ;

Exchange

/* swap/exchange the places/indexes of two values in the array */
array int arrayofInts ;
array Add arrayOfInts 2 3 15 7 3 7 ;
array Exchange arrayOfInts 1 4 ;

IsInArray

/* check whether a value can be found in the array */
bool result = false ;
int searchFor = 15 ;
array int arrayOfInts ;
array IsInArray arrayOfInts searchFor result ;
if result == true
  PrintMessage THE_VALUE_IS_IN_THE_ARRAY ;
else
  PrintMessage THE_VALUE_IS_NOT_FOUND_IN_ARRAY ;
endif
Advertisement