HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Bag Problems again

08-04-2007, 09:52 PM#1
Immoralis
I updated my half-assed bag system so that it saves item position and such but everyone once in a while it duplicates an item or converts an item to an item already in the inventory. Anyone spot any logic errors?

Collapse JASS:
function Trig_JJ_Change_Invent_Actions takes nothing returns nothing
    local unit caster = GetSpellAbilityUnit()
    local integer counter = 1
    local integer itemamount = 0
    local string currentbag = ""
    local string switch2bag = ""
    
    if ( GetSpellAbilityId() == 'A617') then
        if ( GetUnitAbilityLevel(caster,'A617') == 1) then
            call SetUnitAbilityLevel(caster,'A617',2)
            set currentbag = "a"
            set switch2bag = "b"
            
        else
            call SetUnitAbilityLevel(caster,'A617',1)
            set currentbag = "b"
            set switch2bag = "a"
        endif
        
        loop
        exitwhen (counter > 6)
            if (GetItemTypeId(UnitItemInSlotBJ(caster,counter)) != null) then
                set itemamount = itemamount + 1
            endif
            set counter = counter + 1
        endloop
    
        call SetHandleInt(caster, currentbag + "itemamount", itemamount)
        
        if (itemamount != 0) then
            set counter = 0
            
            loop
            exitwhen(counter > itemamount - 1 )
            
                call SetHandleInt(caster, currentbag + "slot" + I2S(counter), GetItemTypeId(UnitItemInSlot(caster,counter)))
                if (GetItemType(UnitItemInSlot(caster,counter)) == ITEM_TYPE_CHARGED) then
                    call SetHandleInt(caster, currentbag + "charges" + I2S(counter), GetItemCharges(UnitItemInSlot(caster,counter)))
                else
                    call SetHandleInt(caster, currentbag + "charges" + I2S(counter), -1)
                endif
                set counter = counter + 1
                
            endloop
        endif 

        set counter = 1
        loop
        exitwhen (counter > 6)
            call RemoveItem( UnitItemInSlotBJ(caster, counter) )
            set counter = counter + 1
        endloop
        
         if ( GetUnitAbilityLevel(caster,'A617') == 2) then
            call SetUnitAbilityLevel(caster,'A616',2)
        else
            call SetUnitAbilityLevel(caster,'A616',1)
        endif
    
        if (GetHandleInt(caster, switch2bag + "itemamount") != 0) then
            set counter = 0
            set itemamount = 0
        
            loop
            exitwhen (counter>6)
                call DisplayTextToForce(GetPlayersAll(),I2S(GetHandleInt(caster, switch2bag + "slot" + I2S(counter))))
                set counter = counter + 1
            endloop
            set counter = 0
        
            loop
            exitwhen(itemamount > GetHandleInt(caster, switch2bag + "itemamount") or counter > 5)
                
                if ( GetHandleInt(caster, switch2bag + "slot" + I2S(counter)) != 0 ) then
                    //call DisplayTextToForce(GetPlayersAll(),"ItemID " + I2S(GetHandleInt(caster, switch2bag + "slot" + I2S(currentslot))))
                    call UnitAddItemById(caster,GetHandleInt(caster, switch2bag + "slot" + I2S(counter)))
                    set itemamount = itemamount + 1
                    
                    if (GetItemType(UnitItemInSlot(caster,counter)) == ITEM_TYPE_CHARGED) then
                        call SetItemCharges(UnitItemInSlot(caster,counter),GetHandleInt(caster, switch2bag + "charges" + I2S(counter)))
                    endif
                else
                    if (counter < 5) then
                        call UnitAddItemById(caster,'mnst')
                    endif
                endif
            
                set counter = counter + 1
            endloop
            
            set counter = 1
            
            loop
            exitwhen(counter > 6)
                if ( GetItemTypeId(UnitItemInSlotBJ(caster,counter)) == 'mnst' ) then
                    call RemoveItem( UnitItemInSlotBJ(caster, counter) )
                endif
            set counter = counter + 1
            endloop
        
        endif
    
    endif
    
    set caster = null
endfunction

//===========================================================================
function InitTrig_JJ_Change_Invent takes nothing returns nothing
    set gg_trg_JJ_Change_Invent = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_JJ_Change_Invent, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddAction( gg_trg_JJ_Change_Invent, function Trig_JJ_Change_Invent_Actions )
endfunction
08-04-2007, 11:10 PM#2
botanic
I was doing something using a similar structure and the way i found to fix it was to put in a wait so that everything is not swapped instantaneously

I did it in GUI however and cant help you with JASS (i can read it kind of just not to well) but i did come to the conclusion that sometimes items take a fraction of a second to initialize which can lead to non working swaps and duplicate items. ;/

With my test it seemed that .2 was a good number as I never have had that problem since i added in a .2 wait. However faster would probably work yet could also add to the possibility of an initialization delay.

EDIT

If adding in delay doesn't fix the problem i can try to look over the JASS just it is slow for me to read it and look for errors ;/
08-04-2007, 11:42 PM#3
Immoralis
i try to avoid waits cause people can pick up items in between the waits and that would mess it up
08-05-2007, 01:45 AM#4
botanic
well im willing to bet item lagg is the culprate...