• 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.

[JASS] Count items of same type?

Status
Not open for further replies.
Level 7
Joined
Mar 22, 2010
Messages
228
i was planning to make a trigger that counts an item in a units inventory only of the same type...
actually i know how but i want to ask some suggestion from you guys if there could be better from using global variables...
 
You can also do this:
  • Actions
    • For each (Integer A) from 1 to 6, do (Actions)
      • Loop - Actions
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • (Item-type of (Item carried by (Triggering unit) in slot (Integer A))) Equal to Inferno Stone
          • Then - Actions
            • Set Count = (Load (Key many) of (Key (Triggering unit)) from Hasht)
            • Hashtable - Save (Count + 1) as (Key many) of (Key (Triggering unit)) in Hasht
          • Else - Actions
    • Game - Display to (All players) the text: (String((Load (Key many) of (Key (Triggering unit)) from Hasht)))
    • Hashtable - Clear all child hashtables of child (Key (Triggering unit)) in Hasht
Edit: Oh wait, you want it in Jass?
 
Easy:

JASS:
function CountHeroItemsOfType takes unit whichUnit, integer itemId returns integer
    local integer index = 0
    local integer count = 0
    loop
        if (GetItemTypeId(UnitItemInSlot(whichUnit, index) == itemId) then
            set count = count + 1
        endif
 
        set index = index + 1
        exitwhen index >= 5
    endloop
 
    return count
endfunction
 
function Actions takes nothing returns nothing
    if CountHeroItemsOfType(GetTriggerUnit(),'ankh') >= 2 then
        //Actions
    endif
endfunction
 
Level 7
Joined
Mar 22, 2010
Messages
228
@Pharaoh_

thanks but i need jass...

@Bribe

nice dude but can you give me some explanation about the jass you gave? i really dont get the point of whichunit...that chews my brain so hardly..:confused:
 
JASS:
function CountHeroItemsOfType takes unit triggeringUnit, integer itemRawCode returns integer // returns the number of specified items
    local integer i = 0
    local integer itemsFound = 0
    loop
        if (GetItemTypeId(UnitItemInSlot(triggeringUnit, i) == itemRawCode) then
            set itemsFound = itemsFound + 1 // Then we've found the item we're looking for; so add one to "items found"
        endif
 // stuff for the loop; nothing special:
        set i = i + 1
        exitwhen i >= 5
    endloop
 
    return itemsFound // returns the value "number of items found of rawcode 'ankh'
endfunction
 
function Actions takes nothing returns nothing
    // function CountHeroItemsOfType requires two parameters; the first one is a unit, the second is the rawcode of the item.
    if CountHeroItemsOfType(GetTriggerUnit(),'ankh') >= 2 then // Inputs GetTriggerUnit() as the parameter unit, 'ankh' as the item rawcode.
        // Actions (do your stuff here)
    endif
endfunction
 
Status
Not open for further replies.
Top