HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Item Stacking (actually JASS Problem)

08-10-2006, 02:39 PM#1
Maszrum
Some time ago I made a simple system used for stacking items which are of "charged" type. Everything went fine when I made it in GUI. A few days ago I decided to make the same thing in JASS. However there is a problem. My system loops through unit's inventory to check if there are two charged items with the same id. As I said it works in GUI. But my JASS function doesn't work every time. It stops when first slot of inventory is occupied by item of not charged type, otherwise it works. It seems as when if/else in the loop returns false, the whole loop returns false as well and ends (also see comments in the code).

Trigger:
Stacking1
Collapse Events
Unit - A unit Acquires an item
Collapse Conditions
(Item-class of (Item being manipulated)) Equal to Charged
Collapse Actions
Collapse For each (Integer A) from 1 to 6, do (Actions)
Collapse Loop - Actions
Collapse For each (Integer B) from 1 to 6, do (Actions)
Collapse Loop - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
Collapse And - All (Conditions) are true
Collapse Conditions
(Item-type of (Item carried by (Hero manipulating item) in slot (Integer A))) Equal to (Item-type of (Item carried by (Hero manipulating item) in slot (Integer B)))
(Integer A) Not equal to (Integer B)
(Item-class of (Item carried by (Hero manipulating item) in slot (Integer A))) Equal to Charged
(Item-class of (Item carried by (Hero manipulating item) in slot (Integer B))) Equal to Charged
Collapse Then - Actions
Item - Set charges remaining in (Item carried by (Hero manipulating item) in slot (Integer A)) to ((Charges remaining in (Item carried by (Hero manipulating item) in slot (Integer A))) + (Charges remaining in (Item carried by (Hero manipulating item) in slot (Integer B))))
Item - Remove (Item carried by (Hero manipulating item) in slot (Integer B))
Else - Actions

"The same" thing in JASS

Collapse JASS:
function StackConditions takes nothing returns boolean
    return GetItemType(GetManipulatedItem()) == ITEM_TYPE_CHARGED
endfunction

function StackActions takes nothing returns nothing
    local unit u
    local integer ia  //integer for first loop
    local integer ib  //integer for second loop
    local item i1
    local integer itemid1
    local item i2
    local integer itemid2

    set u = GetManipulatingUnit()
    set ia = 0
    set ib = 0

    loop
       exitwhen ia > 5
       set i1 = UnitItemInSlot(u, ia)
       set itemid1 = GetItemTypeId(i1)

       loop
          exitwhen ib > 5
          set i2 = UnitItemInSlot(u, ib)
          set itemid2 = GetItemTypeId(i2)
          if (((itemid1 == itemid2) and (ia != ib)) and ((GetItemType(i1) == ITEM_TYPE_CHARGED) and (GetItemType(i2) == ITEM_TYPE_CHARGED))==true) then
             call SetItemCharges(i1, GetItemCharges(i1)+GetItemCharges(i2))
             call RemoveItem(i2)
          else   // I added else to see what happens; it changes nothing
                        //when condition isn't met for the first time, the loop stops
          endif       

          set ib = ib + 1
       endloop

       set ia = ia + 1
    endloop
        
    set u = null
    set i1 = null
    set i2 = null

endfunction

//===========================================================================
function InitTrig_Stack takes nothing returns nothing
    set gg_trg_Stack = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Stack, EVENT_PLAYER_UNIT_PICKUP_ITEM )
    call TriggerAddCondition( gg_trg_Stack, Condition( function StackConditions ) )
    call TriggerAddAction( gg_trg_Stack, function StackActions )
endfunction


What can be causing such a problem?

Oh, and hi everyone by the way...
08-10-2006, 05:07 PM#2
Naakaloh
Have you added debug messages to see what the trigger should be doing?

If there are slots in which the item is null (uninstantiated) and you perform a function like RemoveItem() it might be crashing the thread, since it could be trying to combine 'items' that show up as both having 0 as an ID.

Edit: Hi, welcome to Wc3Campaigns. Also, I was looking over it again and what I said before doesn't look like it should be an issue.
08-10-2006, 05:53 PM#3
PipeDream
set ib = 0 should be inside the outer loop
08-10-2006, 07:20 PM#4
Maszrum
Of course it should... That's why the function worked when proper item was in slot 0. Thanks!