Parent = Category
Children = Data being stored under that Category
The Children are basically a normal 1-dimensional Array:
Child[1] = some data
Child[2] = some data
Child[3] = some data
But they're linked to a Parent:
Parent[1]Child[1] = some data
Parent[1]Child[2] = some data
Parent[1]Child[3] = some data
^That's basically the same thing as a 2-dimensional Array:
Hashtable[1][1] = some data
Hashtable[1][2] = some data
Hashtable[1][3] = some data
If you've ever been creating Array variables for specific players like this:
Player1Hero[1] = Some Unit
Player1Hero[2] = Some Unit
Player1Hero[3] = Some Unit
Player2Hero[1] = Some Unit
Player2Hero[2] = Some Unit
Player2Hero[3] = Some Unit
Stop! What you really want to use is a Hashtable.
Here's what you should
NOT be doing:
-
Bad Example
-
Events
-
Player - Player 1 (Red) leaves the game
-
Player - Player 2 (Blue) leaves the game
-
Player - Player 3 (Teal) leaves the game
-
Conditions
-
Actions
-
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
-
If - Conditions
-
(Triggering player) Equal to Player 1 (Red)
-
Then - Actions
-
Unit - Kill Player1Hero[1]
-
Unit - Kill Player1Hero[2]
-
Unit - Kill Player1Hero[3]
-
Else - Actions
-
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
-
If - Conditions
-
(Triggering player) Equal to Player 2 (Blue)
-
Then - Actions
-
Unit - Kill Player2Hero[1]
-
Unit - Kill Player2Hero[2]
-
Unit - Kill Player2Hero[3]
-
Else - Actions
And here's the same thing using a Hashtable.
There's no need for checking which Player it is, we can simply load our saved Data (Children) using the Player's Number as the Category (Parent):
-
Hashtable Example
-
Events
-
Player - Player 1 (Red) leaves the game
-
Player - Player 2 (Blue) leaves the game
-
Player - Player 3 (Teal) leaves the game
-
Conditions
-
Actions
-
Set Variable PN = (Player number of (Triggering player))
-
Unit - Kill (Load 1 of PN in HeroHashtable)
-
Unit - Kill (Load 2 of PN in HeroHashtable)
-
Unit - Kill (Load 3 of PN in HeroHashtable)
And if you're wondering about how to actually save this stuff to a Hashtable, here's an example of setting it all up:
-
Save to Hash Example
-
Events
-
Time - Elapsed game time is 1.00 seconds
-
Conditions
-
Actions
-
-------- First make sure to actually create the Hashtable and keep track of it using a Hashtable variable: --------
-
Hashtable - Create a hashtable
-
Set Variable HeroHashtable = (Last created hashtable)
-
-------- --------
-
-------- Next, let's create and store Player 1's Heroes to the Hashtable: --------
-
Set Variable PN = 1
-
-------- --------
-
Unit - Create 1 Paladin for Player 1 (Red) at (Center of (Playable map area)) facing Default building facing degrees
-
Hashtable - Save Handle Of (Last created unit) as 1 of PN in HeroHashtable.
-
Unit - Create 1 Archmage for Player 1 (Red) at (Center of (Playable map area)) facing Default building facing degrees
-
Hashtable - Save Handle Of (Last created unit) as 2 of PN in HeroHashtable.
-
Unit - Create 1 Mountain King for Player 1 (Red) at (Center of (Playable map area)) facing Default building facing degrees
-
Hashtable - Save Handle Of (Last created unit) as 3 of PN in HeroHashtable.
-
-------- --------
-
-------- Next, let's create and store Player 2's Heroes to the Hashtable: --------
-
Set Variable PN = 2
-
-------- --------
-
Unit - Create 1 Paladin for Player 2 (Blue) at (Center of (Playable map area)) facing Default building facing degrees
-
Hashtable - Save Handle Of (Last created unit) as 1 of PN in HeroHashtable.
-
Unit - Create 1 Archmage for Player 2 (Blue) at (Center of (Playable map area)) facing Default building facing degrees
-
Hashtable - Save Handle Of (Last created unit) as 2 of PN in HeroHashtable.
-
Unit - Create 1 Mountain King for Player 2 (Blue) at (Center of (Playable map area)) facing Default building facing degrees
-
Hashtable - Save Handle Of (Last created unit) as 3 of PN in HeroHashtable.
I'm using that PN integer as a shortcut to remember the Player's Number. Techniques like that are not necessary and should only be used if you know what you're doing.
Also, note that Hashtables have a limit. I believe you can only sustain 255 Hashtables at a time. That being said, if you master the Hashtable you could potentially use 1 Hashtable for EVERYTHING, or at the very least for more than 1 thing.
Additionally, Hashtables have the ability to save Data (Children) to just about anything. Meaning your Category (Parent) doesn't have to be an Integer, it can be a Unit for example. What does that mean? It means that you can save Data directly to a specific Unit, which is extremely useful for making MUI spell effects and custom stats like Ability Power.
An example of this, whenever a Hero gains a Level their Ability Power stat is increased by 5:
-
Ability Power
-
Events
-
Unit - A unit Gains a level
-
Conditions
-
Actions
-
Set Variable AbilityPower = (Load 1 of (Key (Triggering unit)) from StatHashtable.)
-
Hashtable - Save (AbilityPower + 5) as 1 of (Key (Triggering unit)) in StatHashtable.
AbilityPower is an Integer variable that I use to contain the loaded Data from the Hashtable. It isn't necessary but helps keep things easy to read.
Here's an example without it:
-
Ability Power
-
Events
-
Unit - A unit Gains a level
-
Conditions
-
Actions
-
Hashtable - Save ((Load 1 of (Key (Triggering unit)) from StatHashtable) + 5) as 1 of (Key (Triggering unit)) in StatHashtable.