Here's a brief tutorial explaining almost everything you need to know about arrays. Except working with them, that's more complicated. But once you understand the use of arrays and how to work with them, you will keep using them.
Arrays
Introdution
It might seem strange but it's quite logical. You start at the lowest non-negative number. And that's 0.
What's not logical is that this rule is only applied when dealing with vector arrays and string arrays. However constant arrays, targetname arrays and arrays made using makeArray all start at 1.
And the logical mind would also prefer starting at 1 instead of 0 when creating arrays (which is as simple as pie, just add a box you want to define and declare like you would a variable):
But there are many other ways and many kinds of arrays.
Kinds of Arrays
Examples of vector and string arrays:
Code: Select all
local.myvector = ( 5 6 9 )
local.mystring = "hello"
So:
Code: Select all
local.myvector[0] = 5
local.myvector[1] = 6
local.myvector[2] = 9
local.mystring[0] = "h"
local.mystring[1] = "e"
local.mystring[2] = "l"
local.mystring[3] = "l"
local.mystring[4] = "o"
Constant array:
So:
Code: Select all
local.myarray[1] = 5
local.myarray[2] = 6
local.myarray[3] = 9
Targetname array:
Code: Select all
local.o = spawn script_origin "targetname" "object"
local.b = spawn script_object "targetname" "object"
local.p = spawn script_model "targetname" "object"
So:
Code: Select all
$object[1] = script_origin
$object[2] = script_object
$object[3] = script_model
Example of makeArray (these are 2-dimensional):
The first dimension represents the Xth row, the second dimension represents the Xth value of that row.
Code: Select all
local.myarray = makeArray
1 2 3
4 5 6
7 8 9
endArray
So:
Code: Select all
local.myarray[1][1] = 1
local.myarray[1][2] = 2
local.myarray[1][3] = 3
local.myarray[2][1] = 4
local.myarray[2][2] = 5
local.myarray[2][3] = 6
local.myarray[3][1] = 7
local.myarray[3][2] = 8
local.myarray[3][3] = 9
makeArray may seem confusing at first but it can be very helpful and efficient.
Sizes
Each variable has the .size property. The .size property does not give the total size! Only the size of the dimension you specify.
Code: Select all
local.vector = ( 1 2 3 )
local.string = "hello"
local.example = 5
local.array[1] = 6
local.newarray[1][1] = 7
local.newarray[1][2] = 6
local.newarray[2][1] = 1
local.model = spawn script_model "targetname" "object"
So:
Code: Select all
local.sthing.size = -1 //uninitialized variables have a negative size -1
local.vector.size = 3 //despite starting from 0, the vector still has 3 variables
local.string.size = 5 //despite starting from 0, the string still has 5 variables
local.example.size = 1
local.example[1].size = -1 //uninitialized variables have a negative size -1
local.array.size = 1
local.array[1].size = 1
local.newarray.size = 2
local.newarray[1].size = 2
local.newarray[2].size = 1
$object.size = 1 //there's 1 object
$another_object.size = 0 //inexistent objects' sizes equals 0
local.model.size = 1 //local.model equals the spawned object, so the size is 1 and it must be checked for NULL, because NIL is for variables
Array Flexibility
Arrays don't always need to contain positive numbers, you can use negatives as well. Of course you must be prepared to handle/parse them like that (in your for statements).
The size is logically 1.
Code: Select all
local.myarray[ -1] = 5
local.myarray[ -2] = 16
The size is now 2 and so on.
You can also use strings. I don't know how people call them but I call them stringbox arrays:
Code: Select all
local.array["text"] = 6
local.array["other_text"] = 1
The size is 2...and so on. I think you get the picture.
Array Possibilities
Arrays do not only contain numbers, they can contain vectors, strings, spawned object...etc... everything normal local., group., level. and game. objects can do.
Examples
Code: Select all
local.array["text"] = ( 2 1 5 )
local.ar[1][2] = "hello"
local.ray[ -1] = spawn script_model model "fx/dummy.tik"
local.ray[ -1].origin = ( 25 678 -8 )
//so: local.ray[ -1].origin[1] = 678
Clearing Arrays and Entries
To clear array-entries simple equal that entry to NIL. Of course if that entry has spawned an object, then delete it. To clear an entire array, equal the main variable to NIL.
Examples:
Code: Select all
local.a[1][1] = spawn script_object
local.b[2][3] = 1
local.c[1][1] = 56
local.c[1][2] = 7
local.c[1][3] = 8
local.c[2][1] = 22
Now let's clear them:
Code: Select all
//when an entry represents an object:
local.a[1][1] delete
// if it contains values:
local.b[2][3] = NIL
//if you want to clear all entries:
local.c = NIL
Dimensions
You can set arrays with or without multiple dimensions just like this:
It's that simple. Or you can use makeArray.
However local.a[1] = ?
Because there are 2-dimensions. You can add as many dimensions as you like but keeping it moderate is the key to efficiency.
Imagine:
Then what happens to local.a if I declare an array entry:
Well local.a becomes an array meaning the value 2 is lost (actually overwritten). local.a[1] equals NIL and so do all other uninitialized entries in any dimension of this array.
Like I showed you earlier makeArray arrays always have a minimum of 2 dimensions. The first represents the Xth row, the second, the Xth value on that row.
local.a = makeArray
1 2
3 4
endArray
local.a
[1][2]
Of course, there can be a lot more of dimensions!
Code: Select all
local.a = makeArray
((1::2)::3) 2
"hello" ( 445 65 -9 )
endArray
So:
Code: Select all
local.a[1][1] = Constant Array
local.a[1][1][1] = Constant Array
local.a[1][1][1][1] = 1
local.a[1][1][1][2] = 2
local.a[1][1][2] = 3
local.a[1][2] = 2
local.a[2][1] = String Array
local.a[2][1][0] = "h"
local.a[2][1][1] = "e"
local.a[2][1][2] = "l"
local.a[2][1][3] = "l"
local.a[2][1][4] = "o"
local.a[2][2] = Vector Array
local.a[2][2][0] = 445
local.a[2][2][1] = 65
local.a[2][2][2] = -9