TriggerHappy
...
- Reaction score
- 341
GUI 2d Arrays
By TriggerHappy187
Requirements
Knowledge of variables and arrays.
A brain.
Introduction
2D arrays are very useful in storing data. Now GUI nor JASS comes with a 2d array feature (though vJASS does), but there are ways to simulate them.
First off you need to know what a 2D array is. They allow you to store lots of information with one variable, by utilizing the array correctly. It's a bit hard to explain and grasp at first but hopefully by the end of this tutorial you will understand more of what 2D arrays are.
Concept
The concept of 2D arrays in GUI are to have a base number. Then multiply it by how many arrays you want open, then add the number of the current array.
For example:
set SomeVariable[(1*12) + 1)] = 10
That would utilize 12 slots for each variable. So in order to use all twelve slots you could do something like
set SomeVariable[(1*12) + 1)] = 10
set SomeVariable[(1*12) + 2)] = 50
....
set SomeVariable[(1*12) + 12)] = 100.
Now you may be wondering how this is useful, well it certainly makes creating things with MPI functionality easier.
You also don't have to make a variable for each player or whatever you are using like this.
set SomeVariable1[1] = 10
set SomeVariable1[2] = 50
Imagine doing that for all players..
set SomeVariable1[1] = 10
set SomeVariable2[2] = 10
instead you could do something like this with one variable
set SomeVariable[(1*12) + 1)] = 10
set SomeVariable[(2*12) + 1)] = 10
Examples
The first example we are going to store 3 units owned by the first two players, in one variable.
Code:
Untitled Trigger 001
Events
Map initialization
Conditions
Actions
-------- Player one units. --------
Set Units[((1 x 4) + 1)] = Paladin 0000 <gen>
Set Units[((1 x 4) + 2)] = Archmage 0001 <gen>
Set Units[((1 x 4) + 3)] = Mountain King 0002 <gen>
-------- Player two units. --------
Set Units[((2 x 4) + 1)] = Paladin 0003 <gen>
Set Units[((2 x 4) + 2)] = Archmage 0004 <gen>
Set Units[((3 x 4) + 3)] = Mountain King 0005 <gen>
In this example i'm using they're player numbers to decide what I want to store.
Set Units[((1 x 4) + 1)] = Paladin 0000 <gen>
Is the player number.
Set Units[((1 x 4) + 1)] = Paladin 0000 <gen>
How many slots I want to have open (+1).
Make sure it's always +1 greater than what you want your array size to be.
Set Units[((1 x 4) + 1)] = Paladin 0000 <gen>
What instance in the array it is.
Now just for testing purposes lets display all the names of the stored units, according to their player number. It would look something like..
Code:
Player Group - Pick every player in (All players) and do (Actions)
Loop - Actions
Game - Display to (All players) the text: (Unit 1: + (Name of Units[(((Player number of (Picked player)) x 4) + 1)]))
Game - Display to (All players) the text: (Unit 2: + (Name of Units[(((Player number of (Picked player)) x 4) + 2)]))
Game - Display to (All players) the text: (Unit 2: + (Name of Units[(((Player number of (Picked player)) x 4) + 3)]))
Those actions will display all the names of the units, according to player.
Conclusion
Hopefully this tutorial has taught you at least, the basics of 2D arrays.
Comments+Suggestions please.