| For fun, and to learn more, I started making an inventory system, but its buggering with me lol...
Note (I have functions I2U (converts integer to unit) H2I (obviously, converts a handle to integer (my unit for example)), I also have the function DebugMsg, which you will see, and I have the function trade, which will be included in the 3rd trigger)
oh and PD is used to increase it e.g, call PD(i, 1, true) increases i by 1, false decreases:P
firstly, the GUI Part is this:
Then the Creating Of The Units is:
 JASS: function CreateUnits takes nothing returns nothing
local integer i = 0
local integer inv_slots = udg_inv_slots / udg_slots_per_inv
local rect r = gg_rct_Out_Of_Sight
local integer c_i = 0
local unit array inv
loop
exitwhen i > inv_slots - 1
set inv[i]= CreateUnit(Player(0), 'h001', GetRectCenterX(r), GetRectCenterY(r), 270.00)
call UnitAddItemToSlotById(inv[i], 'I000', 5)
call StoreInteger(udg_c, "storage", "unit"+I2S(i), H2I(inv[i]))
set i = i + 1
endloop
call StoreInteger(udg_c, "storage", "c_i", c_i)
endfunction
function InitTrig_CreateUnits takes nothing returns nothing
set gg_trg_CreateUnits = CreateTrigger( )
call TriggerAddAction( gg_trg_CreateUnits, function CreateUnits)
endfunction
inv_slots, tells me how many units to make, (determined in the GUI part)
you put in you want "40 slots" it
then the actual swapping is:
 JASS: function trade takes unit u1, integer it1, integer sn1, unit u2, integer it2, integer sn2 returns nothing
local integer array inv_item
local integer array inv_e_item
local integer id1 = H2I(u1)
local integer id2 = H2I(u2)
local string id1_s = I2S(id1)
local string id2_s = I2S(id2)
local integer l2 = StringLength(I2S(id1))
local integer i = l2
local string fs1
local string fs2
loop
exitwhen i == l2 - 6
set fs1 = fs1 + SubStringBJ(id1_s, i, i)
set fs2 = fs2 + SubStringBJ(id2_s, i, i)
set i = i - 1
endloop
set fs1 = fs1 + SubStringBJ(id1_s, 1, 1)
set fs2 = fs2 + SubStringBJ(id2_s, 1, 1)
set inv_item[S2I(fs1)] = GetItemTypeId(UnitItemInSlotBJ(u1, sn1))
set inv_e_item[S2I(fs2)] = GetItemTypeId(UnitItemInSlotBJ(u2, sn2))
call DebugMsg("fs1 "+fs1+", f2s "+fs2+", inv_item "+I2S(inv_e_item[S2I(fs2)])+", inv_e_item "+I2S(inv_item[S2I(fs1)]), 120)
call RemoveItem(UnitItemInSlotBJ(u1, sn1))
call RemoveItem(UnitItemInSlotBJ(u2, sn2))
call UnitAddItemToSlotById(u1, it1, sn1)
call UnitAddItemToSlotById(u2, it2, sn2)
endfunction
function PD takes integer inc, integer at, boolean bool returns nothing
if bool == false then
set inc = inc - at
else
set inc = inc + at
endif
endfunction
function swap_up_condition takes nothing returns boolean
return GetItemTypeId(GetManipulatedItem()) == 'I000'
endfunction
function swap_up takes nothing returns nothing
local unit array inv
local integer i = 0
local integer inv_slots = udg_inv_slots / udg_slots_per_inv
local integer pn = GetPlayerId(GetOwningPlayer(GetManipulatingUnit()))+1
local integer c_i = GetStoredInteger(udg_c, "storage", "c_i")+1
local integer array inv_item
local integer array inv_e_item
if c_i > inv_slots-1 then
set c_i = 0
endif
call StoreInteger(udg_c, "storage", "c_i", c_i)
call DisplayTimedTextToPlayer(ConvertedPlayer(pn), 0, 0, 25, "Inventory ["+I2S(c_i)+"]")
loop
exitwhen i > inv_slots - 1
set inv[i] = I2U(GetStoredInteger(udg_c, "storage", "unit"+I2S(i)))
set i = i + 1
endloop
set i = 1
loop
exitwhen i > 4
set inv_item[i] = GetItemTypeId(UnitItemInSlotBJ(udg_Hero[pn], i))
set inv_e_item[i] = GetItemTypeId(UnitItemInSlotBJ(inv[c_i], i))
call trade(udg_Hero[pn], inv_item[i], i, inv[c_i], inv_e_item[i], i)
call PD(i, 1, true)
call DebugMsg(I2S(i), 120)
endloop
endfunction
function InitTrig_SwapUp takes nothing returns nothing
set gg_trg_SwapUp = CreateTrigger()
call TriggerAddAction( gg_trg_SwapUp, function swap_up)
call TriggerAddCondition(gg_trg_SwapUp, Condition(function swap_up_condition))
call TriggerRegisterAnyUnitEventBJ(gg_trg_SwapUp, EVENT_PLAYER_UNIT_USE_ITEM)
endfunction
Anyway that should work, but it doesn't, any idea's
The actual problem is
"my hero" acquires say 2 items, (2 units = 8 inventory slots)
I press the "up" button, and that should give all the items he owns to inv[0] which is the first unit, then give all items from inv[1] to the hero, etc.. but it just doesnt work:(
the items just get removed, its got to do with the trade function, im sure:P |