HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Making an inventory System - Problem, Why Doesn't This Work!! (Jass)

02-21-2007, 10:31 PM#1
Mythic Fr0st
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:

Trigger:
Init
Collapse Events
Map initialization
Conditions
Collapse Actions
Game Cache - Create a game cache from System.w3v
Set c = (Last created game cache)
Visibility - Disable fog of war
Visibility - Disable black mask
Unit - Create 1 Dwarven King for Player 1 (Red) at (Center of (Playable map area)) facing Default building facing (270.0) degrees
Set Hero[(Player number of (Owner of (Last created unit)))] = (Last created unit)
Custom script: call UnitAddItemToSlotById(GetLastCreatedUnit(), 'I000', 5)
Set inv_slots = 8
Set slots_per_inv = 4 //there will only be 4 (2 buttons or up/down inventory, so only 4 left)
Set max_player_index = 1 //not introduced yet



Then the Creating Of The Units is:


Collapse 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]))
            //call ShowUnit(inv[i], false)
            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:

Collapse 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))

    //unit 1, item 1, slot number, unit 2, item 2, slot number 2  
    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 //increases then adds
 local integer array inv_item
 local integer array inv_e_item
    //the items to the unit before it
    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
           //unit 1, item 1, slot number, unit 2, item 2, slot number 2
    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