• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Can't Save Data to Hashtable

Status
Not open for further replies.
After a day woking with hastable, I've got some troubles. So I made this trigger for a little test.

  • Events
    • Player - Player 1 (Red) types a chat message containing -key as A substring
  • Conditions
    • (Length of (Entered chat string)) Equal to 6
  • Actions
    • Set Temp_String = (Substring((Entered chat string), 6, 6))
    • Hashtable - Save (Integer(Temp_String)) as 2 of 2 in ItemData
    • Set Temp_Integer = (Load 2 of 2 from ItemData)
    • Set Temp_String = (String(Temp_Integer))
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • Temp_String Equal to Empty String
      • Then - Actions
        • Game - Display to (All players) the text: no text
      • Else - Actions
        • Game - Display to (All players) the text: Temp_String
Whatever I enter in it, it always return "0", even number or not. I changed the integer to string and it returned "no text". Then I realized there's something wrong with my saving. Can't it be saved to hashtable or did I save it in the wrong way?
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
A Hashtable is used to save data to a certain object, from the way I see it, you have no reference of an object, it will return as null (0).

Object such as Unit, Ability, Item, etc.

For example:
You want to save an Integer value '5' to a Unit 'Caster' with a childKey of '0' in 'ItemData' hashtable.
Here's how you save/load it;
  • Actions
    • -------- SAVING --------
    • Set Object = (Triggering unit)
    • Custom script: set udg_parentKey = GetHandleId(udg_Object)
    • Set childKey = 0
    • Set SavedValue = 5
    • Hashtable - Save SavedValue as childKey of parentKey in ItemData
    • -------- ///////////////////// --------
    • -------- LOADING --------
    • Set Object = (Triggering unit)
    • Custom script: set udg_parentKey = GetHandleId(udg_Object)
    • Set childKey = 0
    • Set SavedValue = (Load childKey of parentKey from ItemData)
    • Game - Display to (All players) the text: (String(SavedValue))
Now tell me, what do you intend to do ?
Perhaps we can re-construct the trigger - I think we don't even have to use Hashtable for this.

Solution:
  • String Comparison
    • Events
      • Player - Player 1 (Red) types a chat message containing -key as A substring
    • Conditions
    • Actions
      • Set TempString = (Entered chat string)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Length of TempString) Equal to 6
        • Then - Actions
          • Set TempInt = (Player number of (Triggering player))
          • Set SavedValue[TempInt] = (Integer((Substring(TempString, 6, 6))))
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (String(SavedValue[TempInt])) Equal to <Empty String>
            • Then - Actions
              • Game - Display to (All players) the text: null
            • Else - Actions
              • Game - Display to (All players) the text: (String(SavedValue[TempInt]))
        • Else - Actions
TempString - String
TempInt - Integer
SavedValue[array] - Integer
 
Last edited:
I made a similar trigger of your example, mine is:

  • Events
    • Player - Player 1 (Red) types a chat message containing -key as A substring
  • Conditions
    • (Length of (Entered chat string)) Equal to 6
  • Actions
    • -------- SAVE CODE --------
    • Set Temp_String = (Substring((Entered chat string), 6, 6))
    • Set Temp_Integer = (Integer(Temp_String))
    • Set Hashtable_ChildKey = 0
    • Custom script: set udg_Hashtable_ParentKey = GetHandleId( udg_Hero )
    • Hashtable - Save Temp_Integer as Hashtable_ChildKey of Hashtable_ParentKey in ItemData
    • -------- LOAD CODE --------
    • Set Hashtable_ChildKey = 0
    • Custom script: set udg_Hashtable_ParentKey = GetHandleId( udg_Hero )
    • Set Temp_Integer = (Load Hashtable_ChildKey of Hashtable_ParentKey from ItemData)
    • Set Temp_String = (String(Temp_Integer))
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • Temp_String Equal to Empty String
      • Then - Actions
        • Game - Display to (All players) the text: no text
      • Else - Actions
        • Game - Display to (All players) the text: Temp_String
and I still got the 0 value. (Hero is a unit variable and is set by another trigger)

What do I tend to do? Well, initially, I thought hashtable is a kind of variable which uses 2 key to save/load value so I tried to make a item data storage. Example:
Save 45 as 1 of ItemID in ItemData
Save 5 as 2 of ItemID in ItemData
Save 500 as 3 of ItemID in ItemData
Save ... as ... of ItemID in ItemData​
  • ItemID is an integer id of a specific type of item and is set manually
  • 1 is an integer key of "damage"
  • 2 is an integer key of "armor"
  • 3 is an integer key of "HP"
So I can load damage, armor, HP of an item by loading its value from hashtable ItemData.

But this idea seems to be ruined because I still can't save/load value to hashtable.
 
Level 5
Joined
May 6, 2013
Messages
125
First of all, you dont actually need the value of an object handle. A Wc3 hashtable is exactly what you said it was. Therefor, you OP should have worked allready.
Which brings us to the weird part: your usage of the hashtable seems totally fine to me. In fact, i actually ended up copying your trigger just to find that it worked perfectly. Which means the error lies somewhere where we can't see at. Like, did you actually save a valid hashtable into that variable? xD
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Have you set ItemData to a Hashtable yet ?
To elaborate, the variable defaults to null. For you to use a hashtable you first need to make one. In an initialization trigger you need to set the variable to a hashtable. In GUI this requires you running the action to make the hashtable before you can set a variable to the last created hashtable.
 
To elaborate, the variable defaults to null. For you to use a hashtable you first need to make one. In an initialization trigger you need to set the variable to a hashtable. In GUI this requires you running the action to make the hashtable before you can set a variable to the last created hashtable.

lol i always forget about that for GUI.
 
Status
Not open for further replies.
Top