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

[Trigger] Count every item TYPE of inventory?

Status
Not open for further replies.
Level 8
Joined
Jun 13, 2010
Messages
344
Hey guys

I need to do so, when I buy an item "Craft", it checks if the Working Bench (selling unit) has 2 Oakwoods and the recipe in it.
How do I check if a Hero carries 2 of the same item in its inventory?

I still don't know my way all around the conditions, so I have a hard time finding the right ones.
What I've made so far:
  • Working Bench Craft Craftsmanship
    • Events
      • Unit - A unit Sells an item (from shop)
    • Conditions
      • (Item-type of (Sold Item)) Equal to Crafting: Craft
    • Actions
      • Set ItemCounter_Limiter = 0
      • Set ItemCounter_PickUpUnit = (Triggering unit)
      • Set ItemCounter_Inventory = (Size of inventory for ItemCounter_PickUpUnit)
      • For each (Integer ItemCounter_Loop) from 1 to ItemCounter_Inventory, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Item level of (Item carried by ItemCounter_PickUpUnit in slot ItemCounter_Loop)) Equal to 7
              • (ItemCounter_PickUpUnit has an item of type Oakwood) Equal to True
            • Then - Actions
              • Set ItemCounter_Limiter = (ItemCounter_Limiter + 1)
            • Else - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • ItemCounter_Limiter Equal to 2
        • Then - Actions
          • For each (Integer ItemCounter_Loop) from 1 to ItemCounter_Inventory, do (Actions)
            • Loop - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • ItemCounter_Limiter Not equal to 0
                • Then - Actions
                  • Item - Remove (Item carried by ItemCounter_PickUpUnit of type Oakwood)
                  • Set ItemCounter_Limiter = (ItemCounter_Limiter - 1)
                • Else - Actions
          • Hero - Create Oakwood Craftsmanship and give it to ItemCounter_PickUpUnit
        • Else - Actions
For some reason if I remove the
  • (Item level of (Item carried by ItemCounter_PickUpUnit in slot ItemCounter_Loop)) Equal to 7
the trigger will not work.

Thanks for dropping by! :)
 
Last edited:
Level 24
Joined
Aug 1, 2013
Messages
4,657
JASS:
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //
    //  Item functions:
    //
    library ItemFunctions
 
    //Checks if the given unit has items of the given types.
    //Fill the parameters with 0 to have fewer required item types.
    //Works with multiple items of the same type.
    function UnitCheckItems takes unit whichUnit, integer i1, integer i2, integer i3, integer i4, integer i5, integer i6 returns boolean
        local integer array heldItems
        local integer array requestedItems
        local integer index1
        local integer index2
        local integer requestedItemsLength = 6
     
        set requestedItems[0] = i1
        set requestedItems[1] = i2
        set requestedItems[2] = i3
        set requestedItems[3] = i4
        set requestedItems[4] = i5
        set requestedItems[5] = i6
        set heldItems[0] = GetItemTypeId(UnitItemInSlot(whichUnit, 0))
        set heldItems[1] = GetItemTypeId(UnitItemInSlot(whichUnit, 1))
        set heldItems[2] = GetItemTypeId(UnitItemInSlot(whichUnit, 2))
        set heldItems[3] = GetItemTypeId(UnitItemInSlot(whichUnit, 3))
        set heldItems[4] = GetItemTypeId(UnitItemInSlot(whichUnit, 4))
        set heldItems[5] = GetItemTypeId(UnitItemInSlot(whichUnit, 5))
     
        set index1 = 0
        loop
            if requestedItems[index1] == 0 then
                set requestedItemsLength = requestedItemsLength-1
                set requestedItems[index1] = requestedItems[requestedItemsLength]
                if requestedItemsLength == 0 then
                    return true
                endif
            endif
            exitwhen index1 == 5
            set index1 = index1 +1
        endloop
     
        set index1 = 0
        loop
            set index2 = 0
            loop
                exitwhen index2 >= requestedItemsLength
             
                if heldItems[index1] == requestedItems[index2] then
                    set requestedItemsLength = requestedItemsLength-1
                    set requestedItems[index2] = requestedItems[requestedItemsLength]
                    if requestedItemsLength == 0 then
                        return true
                    endif
                    exitwhen true
                endif
             
                set index2 = index2 +1
            endloop
         
            exitwhen index1 == 5
            set index1 = index1 +1
        endloop
     
        return false
    endfunction
 
    function UnitCountItemsOfType takes unit whichUnit, integer itemType returns integer
        local integer result = 0
        local integer i = 0
        loop
            exitwhen i >= 6
         
            if GetItemTypeId(UnitItemInSlot(whichUnit, i)) == itemType then
                set result = result +1
            endif
         
            set i = i +1
        endloop
     
        return result
    endfunction
 
    endlibrary
    //
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The second function is pretty simple (and also exists in GUI iirc).
It just counts the number of items of a certain item-type for a unit.
You could use this to check if the unit has 2 Oakwoods by getting the number of Oakwoods and checking if that is equal to or greater than 2.

The first function is slightly harder but extremely useful in your case.
It checks if the given unit has all the required items.
So in your case, you would check it on "selling unit" with "Oakwood", "Oakwood", "Recipe", 0, 0, 0.
Then it will check if the sellig unit has an oakwood, has another oakwood and has the recipe.
It will return true if it has the required items and false if not. (Pretty straight forward.)

If you are unfamiliar with JASS and how to use it in custom scripts, you might want to copy it from this thread.
(Ignore the horrendous "doesn't work." response after that, I have personally checked that the script worked.)
Also be sure to have JNGP installed because it uses library tags.
 
Level 8
Joined
Jun 13, 2010
Messages
344
If you are unfamiliar with JASS and how to use it in custom scripts, you might want to copy it from this thread.
(Ignore the horrendous "doesn't work." response after that, I have personally checked that the script worked.)
Also be sure to have JNGP installed because it uses library tags.

Wow, that is a looooooooot of triggering just to clarify that I need 3 items.. :O
Does it really have to be this lot of trouble?
Because I mean, if I am going to make this trigger for 40 different recipes... I think I might have to think of something else then, and cut the multiple item part?
 
Level 24
Joined
Aug 1, 2013
Messages
4,657
I think you didn't understand.
You have to copy that script, then you do this:
  • Actions
    • Set UnitCheckItems_Unit = (Triggering unit)
    • Set UnitCheckItems_Items[0] = Oakwood
    • Set UnitCheckItems_Items[1] = Oakwood
    • Set UnitCheckItems_Items[2] = Oakwood Recipe
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • (Evaluate TriggerUnitCheckItems conditions) Equal to True
      • Then - Actions
        • -------- unit has the items --------
      • Else - Actions
        • -------- unit doesnt have the items --------
And you can copy and paste that for every other recipe you want to do.
You dont have to copy the JASS script for each time.
The JASS script is executed in the if condition.

So if you might have 40+ recipes, I will forbid you to use something else.
 
Status
Not open for further replies.
Top