[JASS] Optimize this code

Status
Not open for further replies.
Level 10
Joined
May 27, 2009
Messages
495
I've recently working on some item system but i've been wondering on these great loops on my code.. i tried using hashtable but seems no luck on getting the item's ItemId when it is removed from the game...

Here's the Code:
JASS:
    globals
        private hashtable ht = InitHashtable()
        private hashtable items = InitHashtable()
    endglobals
    
    private function Actions takes nothing returns nothing
        local unit u = GetTriggerUnit()
        local item it = GetManipulatedItem()
        local integer itemid = GetItemTypeId(it)
        local integer i = 0
        local integer int = LoadInteger(ht,0,GetHandleId(u))
        local player p = GetItemPlayer(it)
        local integer tmpint
        local item tmp
        call SaveInteger(ht,0,GetHandleId(u),int + 1) //saves to true allows 1 thread per pickup
        set int = LoadInteger(ht,0,GetHandleId(u))
        if IsItemIdPowerup(itemid) == false and int == 1  then
            call BJDebugMsg("PASS")
            if GetPlayerId(p) > 11 or GetPlayerId(p) < 0 and GetItemType(it) != ITEM_TYPE_CAMPAIGN then
                call SetItemPlayer(it,GetOwningPlayer(u),false)
                call BJDebugMsg("SET PLAYER")
            else
                if GetItemType(it) == ITEM_TYPE_CAMPAIGN then 
                    if p == GetOwningPlayer(u) then
                        loop
                            exitwhen (i > udg_ItemMainCount - 1)
                            if udg_ItemMainDisabled[i] == itemid then
                                call RemoveItem(it)
                                set tmp = UnitAddItemById(u,udg_ItemMain[i])
                                call SetItemPlayer(tmp,GetOwningPlayer(u),false)
                            endif
                            set i = i + 1
                        endloop
                    endif
                else
                    if p != GetOwningPlayer(u) then
                        loop
                            exitwhen (i > udg_ItemMainCount - 1)
                            if udg_ItemMain[i] == itemid then
                                call RemoveItem(it)
                                set tmp = UnitAddItemById(u,udg_ItemMainDisabled[i])
                                call SetItemPlayer(tmp,p,false)
                            endif
                            set i = i + 1
                        endloop
                    endif
                endif
            endif 
        endif
        call SaveInteger(ht,0,GetHandleId(u),0)//resets to 0
        set u = null
        set it = null
    endfunction

What i'm trying to do is optimize the code by trying to remove the loop because it may greatly do some lag / too much use of the processor since the variable contains lots of things...

As i've said, i used table library & hashtable but no luck when I get the item's Id because when it is removed, the variable where I store the Id of the item is also removed (or set to 0) so can anyone help me with this??
 
Level 10
Joined
May 27, 2009
Messages
495
hmm... What i'm trying to do is make the 2 loop parts to be avoided or save them in a separate variable since i'm sure that looping over more than 200 arrays will surely cause the processor to use high memory... but my problem is, whenever the item is removed, the saved value is also deleted.

but for the posted code above, i'm using the loop thing since someone may be able to optimize it :D

btw 'bout the hook thingy... how should it work??
 
Status
Not open for further replies.
Top