I've been trying to make a simple item stacking system.
Here's the code:
The goal is to be able to stack items up to STACK_MAX_AMOUNT, however, this system has quite strange behaviour:
When I only pick up potions, it works.
If I pick up other kinds of potions, they are stacked with original potions. For example if I have 10 life potions and I pick up 5 mana potions, I will have 15 life potions.
If I pick up any other kinds of items, they are simply deleted.
Could someone find the error I've missed?
Here's the code:
JASS:
scope ItemStacking initializer init
globals
private constant integer STACK_MAX_AMOUNT = 100
endglobals
private function onPickup takes nothing returns boolean
local integer i = 0
local item inSlot
local item picked = GetManipulatedItem()
local unit u = GetManipulatingUnit()
local integer addedCharges
loop
exitwhen i == 6
set inSlot = UnitItemInSlot(u, i)
if GetItemType(inSlot) == GetItemType(picked) then
set addedCharges = IMinBJ(STACK_MAX_AMOUNT-GetItemCharges(inSlot), GetItemCharges(picked))
call SetItemCharges(inSlot, GetItemCharges(inSlot) + addedCharges)
call SetItemCharges(picked, GetItemCharges(picked) - addedCharges)
if GetItemCharges(picked) == 0 then
call RemoveItem(picked)
set u = null
set picked = null
set inSlot = null
return false
endif
endif
set i = i + 1
endloop
set u = null
set picked = null
set inSlot = null
return false
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_PICKUP_ITEM )
call TriggerAddCondition(t, Condition(function onPickup))
set t = null
endfunction
endscope
The goal is to be able to stack items up to STACK_MAX_AMOUNT, however, this system has quite strange behaviour:
When I only pick up potions, it works.
If I pick up other kinds of potions, they are stacked with original potions. For example if I have 10 life potions and I pick up 5 mana potions, I will have 15 life potions.
If I pick up any other kinds of items, they are simply deleted.
Could someone find the error I've missed?