Tutorial GUI 2D Arrays Tutorial

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.
 

Romek

Super Moderator
Reaction score
963
Explain in more detail 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.
Doesn't suffice.
 

Romek

Super Moderator
Reaction score
963
You're explaining how to use them in GUI.
Not what they actually are.

A simple way to explain would be to use a table.
 

RaiJin

New Member
Reaction score
40
i still dont understand what the multiplication is for?

SomeVariable[12+1] = 50

wouldn't that work too?
 

Romek

Super Moderator
Reaction score
963
No, it wouldn't.
They'd collide if you want for example [5][2] and [6][1].
 

GooS

Azrael
Reaction score
154
Array:

Code:
[X][X][X]

2D Array:

Code:
[X][X][X]
[X][X][X]
[X][X][X]
An array within a box of an array.

| : Hero Level, - : Ability Level (vertical/Horizontal)

Code:
    1    2    3
1 [100][120][140]
2 [200][240][280]
3 [300][360][400]

Fetching damage of ability based on both level of ability and hero, not using level multiplication etc.

One way it could be used I guess.

//==GooS

On topic, great of you to submit this :thup:

PS: Never used 2D array in WE, but I guess the principle should be the same as PHP and MySQL so above said example should work.
 

Romek

Super Moderator
Reaction score
963
No, it wouldn't.
Maybe actually use [x][y] in the demonstrations, and show how they're used.
Going straight into what you're doing can be confusing.
A good way to explain could be to use a table.
Each row is another dimension.

Also, explain the array limit with 2D arrays...

Edit: GooS explained 2D arrays there. See the difference between what you did and what he did? :rolleyes:
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top