Arrays use an array index to allow you to select which slot to read or write data from or to. An array may have more than one array index.
In BASIC dialects, including PlayBasic, the array index is almost always contained in parenthesis ( ( ) ). In game programming languages that use a form C-Syntax, the array index is contained in brackets ( [ ] ).
; BASIC Style
MyArray(5)
// C-Syntax Style
MyArray[5];
Multiple Array Indexes
This page only covers the general concept of array indexes and only uses 1D arrays as example. For information on arrays with more than one array index, see Array Dimensions.
Indirect Array Index Reference
You can reference the array index directly with a number :
someArray(2) = 34
or, you can use several ways of indirectly referencing the array index.
Array Index Offset and Other Math
The array index can be the product of a mathematical expression :
someArray(1+1) = 34
someArray(3-1) = 34
When the array index is referenced in this way, it is known as an array offset.
Any other mathematical expression that produces a integer can also be used as the array index:
someArray(2*1) = 34
someArray(4/2) = 34
A Variable as an Array Index
You can use a variable as an array index so long as the variable contains a number.
someVar = 2
someArray(someVar) = 34
Return Value of Functions
If a function returns an integer, this number can be used as the array index.
someArray(INT(2.2)) = 34
someArray(VAL("2")) = 34
The array index can also be randomly generated with the RndRange() or the Rnd() functions. This can be useful for generating random names and other random things that require an array :
DIM names(5) AS STRING
names(1) = "Dave"
names(2) = "Bruno"
names(3) = "Bob"
names(4) = "Bill"
names(5) = "Mark"
PRINT names(INT(RND(1)*5)+1))
Related Pages
| Categories: Arrays |