A good way to learn how to use array might be to start with one dimensional array.
A one 1D-array is just like a simple indexed list.
Array are usefull because they provide a unique name (the name of the array) for more than one value.
Classical variables (instance, global, local, etc) don't, it's always one name <-> one value.
Using one name for many values helps to lessen the number of variable.
Imagine a list of enemy. With variable you'd have to do stuff like:
global variable enemy1 = "wolf"
global variable enemy2 = "bear"
global variable enemy3 = "cactus" // why not?
etc
There's no advantage and many drawback to this method
I'd use Array like this
+On Start of layout
-> enemy set size to (3,1,1)
-> enemy.At(0) = "wolf"
-> enemy.At(1) = "bear"
-> enemy.At(2) = "cactus"
(note that array are 0-based)
This way you can do stuff like
+On Start of layout
-> Text: set Text to "There are "&enemy.width&" types of enemy in this area:"&newline
+for each X element
-> Text append " -"&enemy.CurValue&newline
Note that:
- using foreach X element is the same as doing:+for "" from 0 to enemy.width-1
-> Text append " -"&enemy.At(loopindex)&newline
- using enemy.CurValue is the same as using enemy.At(enemy.CurX)
Once you're used to work with 1D array, you can begin to use 2D arrays. For instance you could store the area index in X index and use the Y indexing for the list of enemies.
Like
+On Start of layout
-> enemy set size to (2,3,1)
-> enemy.At(0,0) = "wolf"
-> enemy.At(0,1) = "bear"
-> enemy.At(0,2) = "cactus"
-> enemy.At(1,0) = "pony"
-> enemy.At(1,1) = "jar jar binks"
-> enemy.At(1,2) = "your stepmother"
And you could go really far with this idea if you want to even store the name of the area and some characteristics.
There you can do it by organising your array such as the 2 first Y indexes are for these data and the rest is for the list of enemies
+On Start of layout
-> enemy set size to (2,5,1)
-> enemy.At(0,0) = "The Shire" //name of the area
-> enemy.At(0,1) = "mostly cloudy" // type of weather
-> enemy.At(0,2) = "wolf"
-> enemy.At(0,3) = "bear"
-> enemy.At(0,4) = "cactus"
-> enemy.At(1,0) = "DreamLand" //name of the area
-> enemy.At(1,1) = "sunny" // type of weather
-> enemy.At(1,2) = "pony"
-> enemy.At(1,3) = "jar jar binks"
-> enemy.At(1,4) = "your stepmother"
Or you could build a 3D array to split things up a bit more
+On Start of layout
-> enemy set size to (2,2,3)
-> enemy.At(0,0,0) = "The Shire" //name of the area
-> enemy.At(0,0,1) = "mostly cloudy" // type of weather
-> enemy.At(0,1,0) = "wolf"
-> enemy.At(0,1,1) = "bear"
-> enemy.At(0,1,2) = "cactus"
-> enemy.At(1,0,0) = "DreamLand" //name of the area
-> enemy.At(1,0,1) = "sunny" // type of weather
-> enemy.At(1,1,0) = "pony"
-> enemy.At(1,1,1) = "jar jar binks"
-> enemy.At(1,1,2) = "your stepmother"
(overkill isn't it?)
Anyway in the end the most important thing to consider when using array is how you'll organize it.
And also, I never set so much data by end. I always end up putting all that in a string and parsing it (via tokenat and tokencount)
But that's another story (: