• Check out the results of the Techtree Contest #19!
  • 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.
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!

[Solved] How to locate a specific item variable?

Level 10
Joined
Jun 13, 2010
Messages
398
Say I have an item in my inventory named Item[1] and it has a bunch of variables connected to it like Item_Strength[1], Item_Agility[1], Item_Level[1] etc.

I want to check the units inventory for what items the unit has. Then I want to find what variable is set to the given item.

The only thing I can come up with is either adding custom value like in the example here 1 - check for custom value for item and do find Item_Strength[CustomValue].

Or go through For every item in unit slot go through value 1-1000 if it is this item then good lol.

Is the custom value the safest bet or am I missing something very simple?
 
You can use hashtables. Each item has a unique handleId, so you could use the handleId as parent key and things like "Strenght", "Agility", etc. as child keys to load your values.

The other, probably easier solution since you already have arrays, is to use a bit of math.
Either store the owner of the items in unit array and when you need to check unit's items, find the unit first in array to determine its index.
If you are using a unit indexer, then you could leverage custom value and skip the unit array part.

In any case, be it index or custom value, use that number -1 and multiplied by 6 (or whatever number of slots your inventory has) as index offset for Item[] array. The following 6 slots (indexex from +1 up to +6) in the item array would be items of the unit.

A unit with index/CV = 1 will have have index offset as "(1 - 1) * 6 = 0".
So offset = 0 and unit's inventory is placed on indices offset + [1, 6], so indexes 1 up to 6.

A unit with index/CV = 170 will have index offset equal to 1014 and its items in the array would be placed on indices 1015 up to 1020.
 
You can use hashtables. Each item has a unique handleId, so you could use the handleId as parent key and things like "Strenght", "Agility", etc. as child keys to load your values.

The other, probably easier solution since you already have arrays, is to use a bit of math.
Either store the owner of the items in unit array and when you need to check unit's items, find the unit first in array to determine its index.
If you are using a unit indexer, then you could leverage custom value and skip the unit array part.

In any case, be it index or custom value, use that number -1 and multiplied by 6 (or whatever number of slots your inventory has) as index offset for Item[] array. The following 6 slots (indexex from +1 up to +6) in the item array would be items of the unit.

A unit with index/CV = 1 will have have index offset as "(1 - 1) * 6 = 0".
So offset = 0 and unit's inventory is placed on indices offset + [1, 6], so indexes 1 up to 6.

A unit with index/CV = 170 will have index offset equal to 1014 and its items in the array would be placed on indices 1015 up to 1020.
Thanks for taking your time to help!

The areay variables were just examples - haven't started yet as I want to make less trouble for myself by making it right the first time. 😅

In your example with the hashtables: if an item is the parent key - how would i find the parent key then? If the item is on the ground and I want to know, what the integer for the parent key is, how do I do that? 😁
 
Well, first of all, you would need to initialize the hashtable:
  • Map Ini
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Hashtable - Create a hashtable
      • Set VariableSet ItemsHashtable = (Last created hashtable)
Then you need to get reference to the item somehow. If it is items in inventory, then loop through unit's inventory. If it is on ground, then pick all items in region and loop through them. Or, if it is some item of interest, then when it is created, you track it in some array variable.
To load data from hashtable, you use the "Hasthable - Load {type} Value" function which requires two keys - the child and parent key (the format is "Load childKey of parentKey from hashtable").
For keys, you use "Hashtable - Get String ID" and "Hashtable - Get Handle ID" functions.
  • HandleID is the unique number of an object in game.
  • StringID creates a number from string - do note that this is case sensitive. There will be different StringID for "myKey" vs "MyKey".

Example:
  • Item - Pick every item in (Playable map area) and do (Actions)
    • Loop - Actions
      • Set VariableSet Agi = (Load (Key Agility.) of (Key (Picked item).) from ItemsHashtable.)
      • Set VariableSet Int = (Load (Key Intelligence.) of (Key (Picked item).) from ItemsHashtable.)
      • Set VariableSet Str = (Load (Key Strength.) of (Key (Picked item).) from ItemsHashtable.)
In the above, child keys Agility/Intelligence/Strength are StringIDs, while (Key (Picked item)) is HandleID.
When using the "Get Handle ID" function, there is a huge list of functions as it contains functions for all possible types (i.e. units, destructibles, items, etc.)
This is how you could iterate through inventory:
  • For each (Integer A) from 1 to 6, do (Actions)
    • Loop - Actions
      • Set VariableSet Agi = (Load (Key Agility.) of (Key (Item carried by your_unit in slot (Integer A)).) from ItemsHashtable.)
      • Set VariableSet Int = (Load (Key Intelligence.) of (Key (Item carried by your_unit in slot (Integer A)).) from ItemsHashtable.)
      • Set VariableSet Str = (Load (Key Strength.) of (Key (Item carried by your_unit in slot (Integer A)).) from ItemsHashtable.)
I believe the "Get Handle ID" function does not allow you to reference a variable - for those cases you will need a bit of JASS to get it:
  • Set VariableSet MyItem = (Last created item)
  • Custom script: set udg_HandleId = GetHandleId(udg_MyItem)
  • Set VariableSet Agi = (Load (Key Agility.) of HandleId from ItemsHashtable.)
The HandleId variable is of type 'integer'.
 
Last edited:
Well, first of all, you would need to initialize the hashtable:
  • Map Ini
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Hashtable - Create a hashtable
      • Set VariableSet ItemsHashtable = (Last created hashtable)
Then you need to get reference to the item somehow. If it is items in inventory, then loop through unit's inventory. If it is on ground, then pick all items in region and loop through them. Or, if it is some item of interest, then when it is created, you track it in some array variable.
To load data from hashtable, you use the "Hasthable - Load {type} Value" function which requires two keys - the child and parent key (the format is "Load childKey of parentKey from hashtable").
For keys, you use "Hashtable - Get String ID" and "Hashtable - Get Handle ID" functions.
  • HandleID is the unique number of an object in game.
  • StringID creates a number from string - do note that this is case sensitive. There will be different StringID for "myKey" vs "MyKey".

Example:
  • Item - Pick every item in (Playable map area) and do (Actions)
    • Loop - Actions
      • Set VariableSet Agi = (Load (Key Agility.) of (Key (Picked item).) from ItemsHashtable.)
      • Set VariableSet Int = (Load (Key Intelligence.) of (Key (Picked item).) from ItemsHashtable.)
      • Set VariableSet Str = (Load (Key Strength.) of (Key (Picked item).) from ItemsHashtable.)
In the above, child keys Agility/Intelligence/Strength are StringIDs, while (Key (Picked item)) is HandleID.
When using the "Get Handle ID" function, there is a huge list of functions as it contains functions for all possible types (i.e. units, destructibles, items, etc.)
This is how you could iterate through inventory:
  • For each (Integer A) from 1 to 6, do (Actions)
    • Loop - Actions
      • Set VariableSet Agi = (Load (Key Agility.) of (Key (Item carried by your_unit in slot (Integer A)).) from ItemsHashtable.)
      • Set VariableSet Int = (Load (Key Intelligence.) of (Key (Item carried by your_unit in slot (Integer A)).) from ItemsHashtable.)
      • Set VariableSet Str = (Load (Key Strength.) of (Key (Item carried by your_unit in slot (Integer A)).) from ItemsHashtable.)
I believe the "Get Handle ID" function does not allow you to reference a variable - for those cases you will need a bit of JASS to get it:
  • Set VariableSet MyItem = (Last created item)
  • Custom script: set udg_HandleId = GetHandleId(udg_MyItem)
  • Set VariableSet Agi = (Load (Key Agility.) of HandleId from ItemsHashtable.)
The HandleId variable is of type 'integer'.
That makes a lot of sense to me.

I figured I would have to run an integer loop/pick every item in region etc and run through them all to filter out the right item. I just wasn't sure if there was an easier way.

What I want to do is add stats to a Hero using triggers. So when an item is picked up or stats are altered through a skill point (+5% to all stats) or whatever, I need to take the base stats of the Hero and locate all the bonus stats from every item in the inventory (and its custom stored stats from a hashtable) and set the stats equal to all these stats combined.

I have no understanding how much memory it takes for the game to run through all this. 😅 Or if it will be no problem

So a hashtable array for every player number representing the players hero. And this is easy to save in say the Codeless save/load system for items in the inventory and the attached stats in the hashtable? 😬
 
If you want an item to add stats, then I think it makes more sense to store item-type as key, not an actual item. I imagine that an item always gives a set/constant stat? Like Mantle of Intelligence items will always give +5% int and Stave of Strength items will always give +5% str? If yes, then you want to bind data to an item-type, not an actual item.
 
If you want an item to add stats, then I think it makes more sense to store item-type as key, not an actual item. I imagine that an item always gives a set/constant stat? Like Mantle of Intelligence items will always give +5% int and Stave of Strength items will always give +5% str? If yes, then you want to bind data to an item-type, not an actual item.
When an item drops, it will have stats according to level of item and a range roll. So the same item will almost never roll the same stats. So I need it to be very item specific.

So I believe it has to be linked to a specific item in the hashtable? Like giving the item a custom value and then save the stats to the item as child key under the parent key which is the custom value of the item? 😬😅
 
Back
Top