• 🏆 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!

Inventory Load Problem

Status
Not open for further replies.
Level 20
Joined
Jul 6, 2009
Messages
1,885
I've got a problem with some item manipulating trigger.
I saved units' items using hashtables:

  • For each (Integer A) from 1 to 6, do (Actions)
    • Loop - Actions
      • Hashtable - Save Handle Of(Item carried by (Picked unit) in slot (Integer A)) as (9 + (Integer A)) of (Key (Picked unit)) in Save_Hashtable
Now i want to make their inventories excatly as they were,so i used this trigger:

  • For each (Integer A) from 1 to 6, do (Actions)
    • Loop - Actions
      • Item - Remove (Item carried by (Picked unit) in slot (Integer A))
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Load (9 + (Integer A)) of (Key (Picked unit)) in Save_Hashtable) Not equal to No item
        • Then - Actions
          • Hero - Create (Item-type of (Load (9 + (Integer A)) of (Key (Picked unit)) in Save_Hashtable)) and give it to (Picked unit)
        • Else - Actions
          • Do nothing
The problem is that when i load units' inventory it becomes empty,i also deleted "Item - Remove" afterwards so i can see if hashtables successfully saved and loaded items,and they did.
So how can i make them properly load inventories? (I hope you get point)
+rep for help
 
Level 37
Joined
Mar 6, 2006
Messages
9,240
Look, you remove the item. Then the game tries to check what item type it is, an item that doesn't exist. What do you think the result is? Will the code somehow guess the item type?

Let's say you hide a fork under your pillow. Then after a while you remove the fork and throw it away. Then you ask your friend to go take a look under the pillow an then tell you what item was there a while ago. How could he know?
 
Look, you remove the item. Then the game tries to check what item type it is, an item that doesn't exist. What do you think the result is? Will the code somehow guess the item type?
Well, this is not entirely true. I make a trigger that saves the x of a location and its y, but, even if I use in the same trigger the script to remove the location, I can still load the location afterwards in another trigger. Why do x and y still correspond?
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Save the item type and charges in a hashtable in the form of integers and then load them when needed. Do not save actual items unless you hide them off the unit otherwise they will be the same as passing null to the get item type function. Think back to your experiences with OOP. When you remove an item you equivently destroy its object thus anything getting values from it will return nonsense.
 
Level 20
Joined
Jul 6, 2009
Messages
1,885
Save the item type and charges in a hashtable in the form of integers and then load them when needed. Do not save actual items unless you hide them off the unit otherwise they will be the same as passing null to the get item type function. Think back to your experiences with OOP. When you remove an item you equivently destroy its object thus anything getting values from it will return nonsense.

Ok,i understand.
But how am i supposed to save items using integers,i mean,how will it determine which item should it load? o_O
 
Level 20
Joined
Jul 6, 2009
Messages
1,885
I barely know anything about JASS :(
Can you please make trigger that saves all items for "picked unit" and in next trigger the inventory should look like when it was saved for "picked unit".
And btw,i already made a loop that picks all units...etc,just it needs to do it for picked unit.
 
To learn more about custom scripts, you'll take the gui action and go to Edit -> Convert to custom Text. This way, you will learn how this action is represented in Jass and with a bit of experience, you'll realize what do you need to abstract from the actual code. There is no spell to teach you all this, you need to sit down and learn it with multiple trials.
Another way is to get JassCraft and there are certain scripts there, for combining GUI with Jass. The most helpful to you for this matter will be the ones that begin with a "Get", e.g. "GetUnitTypeId"/"GetOwningPlayer".
Sometimes, certain functions don't appear in GUI, so you will have to get that function from Jass, but you need the whole corresponding-to-the-function code to be in Jass, so you'll need to extend your GUI skills with a bit of Jass functions' arsenal.

P.S. Yeah, Purge, I realized it, thanks :)
 
Something like this:
  • Actions
    • For each (Integer A) from 1 to 6, do (Actions)
      • Loop - Actions
        • Custom script: set udg_IntVar = GetItemTypeId(UnitItemInSlotBJ(GetEnumUnit(), bj_ForLoopAIndex))
        • Hashtable - Save IntVar as (Integer A) of 0 in (Last created hashtable)
        • Set IntVar = (Charges remaining in (Item carried by (Picked unit) in slot (Integer A)))
        • Hashtable - Save IntVar as (Integer A) of 1 in (Last created hashtable)
Bleh, I haven't used hashtables yet so I am not sure how it is supposed to go properly. Something like that. Then you can retrieve it in their respective indexes. The table of 0 has the itemIds from 1-6. The table of 1 has the charges remaining from items 1-6 of the unit.
 
Level 37
Joined
Mar 6, 2006
Messages
9,240
Well, this is not entirely true. I make a trigger that saves the x of a location and its y, but, even if I use in the same trigger the script to remove the location, I can still load the location afterwards in another trigger. Why do x and y still correspond?

It is entirely true.

So you save x and y of a location, remove the location but the x and y are still there. That makes perfect sense. The x and y are just some values, they are not bound to the location in any way. The game does not consider them to be the x and y of that location, they are just some real values.

Let's say you save strength of a hero as an integer to a variable. Then you remove the hero from the game. Will the integer variable retain it's value? Ofcourse it will.

But let's say you save the unit to a variable. Then you remove the unit from the game. Then you try to set the strength of the unit that was saved to a variable into another variable. This won't work since the unit is not there anymore.

See the difference?
 
Level 20
Joined
Jul 6, 2009
Messages
1,885
@Maker,i think you dont understand the point of problem.
This is what i try to do again:
In first trigger i save each item in unit's inventory into hashtable. Then in next trigger that will,for example,happen after 30 seconds,it should make same unit's inventory to look totaly same as it was when first trigger was executed. So i delete each item in unit's inventory by using "Integar A from 1 to 6" loop to remove each item,then i use same loop to fill each empty slot with items i saved in first trigger by loading hashtable.

@Purgeandfire,you don't see problem too.
I can save items,and that works good,the problem is to load them after. I removed my "Remove Item" function later to see if it really saves and loads items in each inventory slot and it works! But it doesn't work with "Remove Item" function as it removes even loaded items. And still this function is needed because i must remove any items that might have been added to unit's inventory between first and second trigger. I remove items because inventory might be changed in any way between first and second trigger,for example,unit can drop items,add new,change each item's position in inventory.
 
Level 37
Joined
Mar 6, 2006
Messages
9,240
I understand the problem perfectly, however you don't seem to see the issue.

Here's the system. It should work, let me know if it bugs somehow. The system leaves blank slots if those exist in inventory, that's why I create the cheese. Change the cheese to some item type you don't use in your map.


  • Save Inventory
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • Trigger - Turn off (This trigger)
      • Game - Display to (All players) the text: Save
      • Set Temp_Loc_1 = (Center of (Playable map area))
      • Set Temp_Group_1 = (Units currently selected by Player 1 (Red))
      • Unit Group - Pick every unit in Temp_Group_1 and do (Actions)
        • Loop - Actions
          • For each (Integer A) from 1 to 6, do (Actions)
            • Loop - Actions
              • Hashtable - Save Handle Of(Item carried by (Picked unit) in slot (Integer A)) as (Integer A) of (Key (Picked unit)) in Item_Hash
              • Hashtable - Save (Charges remaining in (Item carried by (Picked unit) in slot (Integer A))) as ((Integer A) + 6) of (Key (Picked unit)) in Item_Hash
      • Custom script: call DestroyGroup(udg_Temp_Group_1)
      • Custom script: call RemoveLocation(udg_Temp_Loc_1)
      • Wait 1.00 seconds
      • Trigger - Turn on Load Inventory <gen>
  • Load Inventory
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • Trigger - Turn off (This trigger)
      • Game - Display to (All players) the text: Load
      • Set Temp_Loc_1 = (Center of (Playable map area))
      • Set Temp_Group_1 = (Units currently selected by Player 1 (Red))
      • Unit Group - Pick every unit in Temp_Group_1 and do (Actions)
        • Loop - Actions
          • For each (Integer A) from 1 to 6, do (Actions)
            • Loop - Actions
              • Set Temp_Item_1 = (Load (Integer A) of (Key (Picked unit)) in Item_Hash)
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • Temp_Item_1 Not equal to No item
                • Then - Actions
                  • Item - Create (Item-type of Temp_Item_1) at Temp_Loc_1
                  • Item - Set charges remaining in (Last created item) to (Load ((Integer A) + 6) of (Key (Picked unit)) from Item_Hash)
                  • Item - Remove (Item carried by (Picked unit) in slot (Integer A))
                  • Hero - Give (Last created item) to (Picked unit)
                • Else - Actions
                  • Item - Remove (Item carried by (Picked unit) in slot (Integer A))
                  • Hero - Create Cheese and give it to (Picked unit)
          • For each (Integer B) from 1 to 6, do (Actions)
            • Loop - Actions
              • Item - Remove (Item carried by (Picked unit) of type Cheese)
          • Hashtable - Clear all child hashtables of child (Key (Picked unit)) in Item_Hash
      • Custom script: call DestroyGroup(udg_Temp_Group_1)
      • Custom script: call RemoveLocation(udg_Temp_Loc_1)
      • Wait 1.00 seconds
      • Trigger - Turn on Save Inventory <gen>
 

Attachments

  • Inventory_Reset.w3x
    18.3 KB · Views: 36
Status
Not open for further replies.
Top