• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Help me learn and understand hashtables

Status
Not open for further replies.
They store data for later use. They can be used for multiple instances of the same object, without creating conflicts, just like array enhanced variables do. Hashtables can store way more data than indexed systems (the array'd variables I mentioned), up to 408.000, while indexes can store 8192 (8191 if the least index used is 0, e.g. Reals[0]). Hashtables are slower than indexes, but they are easier to work with, especially for GUI.
There are three keys to know about, the hashtable, the handle, the saved value and the label of that value.
Example:
  • Hashtable - Save 0 as (Key banana) of (Key (Triggering unit)) in (Last created hashtable)
Let's break this line down.
The trigger action is "Hashtable - Save Integer". We want to save an integer number to the unit that casts an ability.
"Save 0" -> zero is the value we want to save.
"(Key banana)" -> "banana" is the label, the way we want to name this value to retrieve it afterwards. This is how we will recognize its callback. A plastic bottle has the name of "plastic bottle". In our case, we want to save the number "0" (zero) and call it "banana" to load it, whenever we need to.
"Key(Triggering unit)" -> Where do we want to save the integer number to? To the caster, in our case. This what the third field does.
"(Last created hashtable)" -> Most likely used with a hashtable variable. In what hashtable to we want to save the integer number to? Here is where you pick your hashtable.

Most likely, every spell and/or system has its own hashtable variable, that you create yourself.

Load the value we saved in another trigger:
  • Set LoadInteger = (Load (Key banana) of (Key (Triggering unit)) from (Last created hashtable))
Let's say that we want the effect that, when a unit casts an ability, on the second cast, it will spawn an effect. We need to load the previous integer to increase it by 1. If it is 1 (start the counting from 0, it results two casts).

LoadInteger is an integer variable. Up to the type of value you have saved, you use the appropriate variable. If you save a special effect, you will load it back with a special effect variable. If you save a real value, you will load it back with a real variable, etc.

"banana" is the label we previously spoke of. I order the hashtable to load the "banana" value from it.

"Key (Triggering unit)" is the casting unit, the object where the "banana" labeled value was saved.

and "Last created hashtable" is what I mentioned before.

Finally, before you use a hashtable, you need to initialize it. It's like some machine that needs to be turned on:
  • Trigger
  • Events
    • Map Initialization
  • Conditions
  • Actions
    • Hashtable - Create a hashtable
    • Set Hash1 = (Last created hashtable)
Hash1 is the variabled hashtable I told you about. In this case, our triggers become:
  • Hashtable - Save 0 as (Key banana) of (Key (Triggering unit)) in Hash1
  • Set LoadInteger = (Load (Key banana) of (Key (Triggering unit)) from Hash1
You can also check this tutorial: http://www.hiveworkshop.com/forums/trigger-gui-editor-tutorials-279/hashtables-mui-133407/
 
Level 29
Joined
Mar 10, 2009
Messages
5,016
I love hashtables as they're easier than indexing systems, the only problem I encounter for them is that they cant stack on the same ID...say you cast a spell on the target and it's ID is target unit, then you save an effect, then another will cast, with the same ID, the previous effect cant be recovered forever and that's bad :(...

Luckily, there is lastcreated unit as ID and timers in JASS, so stacking is possible...
 
Status
Not open for further replies.
Top