HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

BagSystem Crashing

08-27-2006, 04:25 PM#1
Thunder_Eye
HandleLocalVars


Collapse JASS:
function H2I takes handle h returns integer  
    return h 
    return 0 
endfunction
 
function LocalVars takes nothing returns gamecache  
    return InitGameCache("jasslocalvars.w3v") 
endfunction

function SetUnitBag takes handle subject, string name, handle value returns nothing  
    if value==null then  
        call FlushStoredInteger(LocalVars(),I2S(H2I(subject)),name)  
        else  
        call StoreInteger(LocalVars(), I2S(H2I(subject)), name, H2I(value))  
    endif 
endfunction

function SetUnitCurrentBag takes handle subject, string name, handle value returns nothing  
    if value==null then  
        call FlushStoredInteger(LocalVars(),I2S(H2I(subject)),name)  
        else  
        call StoreInteger(LocalVars(), I2S(H2I(subject)), name, H2I(value))  
    endif 
endfunction

function SetBagItemId takes handle subject, string name, integer value returns nothing  
    if value==0 then  
        call FlushStoredInteger(LocalVars(),I2S(H2I(subject)),name)  
        else  
        call StoreInteger(LocalVars(), I2S(H2I(subject)), name, value)  
    endif 
endfunction

function SetUnitItemId takes handle subject, string name, integer value returns nothing  
    if value==0 then  
        call FlushStoredInteger(LocalVars(),I2S(H2I(subject)),name)  
        else  
        call StoreInteger(LocalVars(), I2S(H2I(subject)), name, value)  
    endif 
endfunction

function GetUnitBag takes handle subject, string name returns item
    return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
    return null
endfunction

function GetUnitCurrentBag takes handle subject, string name returns item
    return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
    return null
endfunction

function GetBagItemId takes handle subject, string name returns integer  
    return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name) 
endfunction

function GetUnitItemId takes handle subject, string name returns integer  
    return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name) 
endfunction 

function FlushHandleLocals takes handle subject returns nothing  
    call FlushStoredMission(LocalVars(), I2S(H2I(subject)) ) 
endfunction



Collapse JASS:
function Trig_Backpack1_Conditions takes nothing returns boolean
    return GetItemTypeId(GetManipulatedItem()) == 'I000'
endfunction
function Trig_Backpack1_Actions takes nothing returns nothing
    local item i = GetManipulatedItem()
    local item j = null
    local unit u = GetManipulatingUnit()
    local integer k = 0
//Clean Unit    
    call FlushHandleLocals(u)
//Attach the opened bag to unit
    call SetUnitCurrentBag(u, "current", i)
//Attach possible bags in slot 1-6 to the unit
    loop
        exitwhen k == 6
        if GetItemTypeId(UnitItemInSlot(u, k)) == 'I000' then
            call SetUnitBag(u, I2S(k), UnitItemInSlot(u, k))
        endif
        set k = k + 1
    endloop
    set k = 0
//Move Bags from unit    
    loop
        exitwhen k == 6
        call SetItemPosition(GetUnitBag(u,I2S(k)), 100, 100)
        set k = k + 1
    endloop
    set k = 0
//Save items id's on the unit in slot 1-6    
    loop
        exitwhen k == 6
        call SetUnitItemId(u, I2S(k), GetItemTypeId(UnitItemInSlot(u, k)))
        set k = k + 1
    endloop
    set k = 0
//Remove items in slot 1-6    
    loop
        exitwhen k == 6
        call RemoveItem(UnitItemInSlot(u, k))
        set k = k + 1
    endloop
//=======================LOAD ITEMS FROM BAG=======================
    set k = 1
//Create "Back" in slot 1    
    set j = UnitAddItemById(u, 'I001')
    call UnitDropItemSlot(u, j, 0)
//Create items from bags slot 2-6 and set their correct position    
    loop
        exitwhen k == 6
        set j = UnitAddItemById(u, GetBagItemId(i, I2S(k)))
        call UnitDropItemSlot(u, j, k)
        set k = k + 1
    endloop
endfunction
//======================================================
function Trig_Back1_Conditions takes nothing returns boolean
    return GetItemTypeId(GetManipulatedItem()) == 'I001'
endfunction
function Trig_Back1_Actions takes nothing returns nothing
    local unit u = GetManipulatingUnit()
    local item i = GetManipulatedItem()
    local item a = GetUnitCurrentBag(u, "current")
    local item j = null
    local integer k = 0
    call FlushHandleLocals(a)
    loop
        exitwhen k == 6
        if GetItemTypeId(UnitItemInSlot(u, k)) == 'I000' then
            call SimError(GetOwningPlayer(u),"Cannot close backpack")
            return
        endif
        set k = k + 1
    endloop
    set k = 1
    loop
        exitwhen k == 6
        call SetBagItemId(a, I2S(k), GetItemTypeId(UnitItemInSlot(u, k)))
        set k = k + 1
    endloop
    set k = 0
    loop
        exitwhen k == 6
        set j = UnitItemInSlot(u, k)
        call RemoveItem(j)
        set k = k + 1
    endloop
    set k = 0
    loop
        exitwhen k == 6
        set j = GetUnitBag(u, I2S(k))
        call UnitAddItem(u, j)
        call UnitDropItemSlot(u, j, k)
        set k = k + 1
    endloop
    set k = 0
    loop
        exitwhen k == 6
        set j = UnitAddItemById(u, GetUnitItemId(u, I2S(k))) 
        call UnitDropItemSlot(u, j, k)
        set k = k + 1
    endloop
endfunction
//===========================================================================
function InitTrig_Backpack1 takes nothing returns nothing
    local trigger t = null

    set t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_USE_ITEM )
    call TriggerAddCondition( t, Condition( function Trig_Backpack1_Conditions ) )
    call TriggerAddAction( t, function Trig_Backpack1_Actions )
    
    set t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_USE_ITEM )
    call TriggerAddCondition( t, Condition( function Trig_Back1_Conditions ) )
    call TriggerAddAction( t, function Trig_Back1_Actions )
    
    set t = null
endfunction

I000 = Backpack Item
I001 = Back to Unit Item

function Backpack1 = when unit opens bag
function Back1 = when unit closes bag

Everything works except these two things:
1. The "Opened" bag never gets moved back to the unit.
2. If I had an item in the units inventory before I opened bag the game will crash when I close it (won't crash when inventory was empty before openeing bag)

I am aware that the code sucks, I will fix that when it works (merging loops etc)
08-27-2006, 08:43 PM#2
MeanMachine
I just don't know why you have different cache function wich do the same thing. And by the way, if you wrote that as one loop isntead of 100 you wouldn't it would have been a lot better. Now to answer you questions :

1.The store bag and store item id do the same thing - they store an integer. And, they store that integer with the same mission key( I2S(H2I(u)) ) and the same key(I2S(k)). So the second time you store them it overwrites the first.
-Solution : just store all the items at the same time, bags or not. Instead of destroying them just put all of them somewhere.

2. Same cause as 1, thou a different result. When it tries to get a bag it doesn't know if the thing it gets is stored by SetUnitItemId or StoreBag, because they both store an integer with the same keys. It returns the item rawcode as an item pointer and when the functions attemts to return that item the game crashes.
-Solution : same as 1. Give back the items you put away earlier.

And please, Use a single loop damn it.
08-27-2006, 08:49 PM#3
Thunder_Eye
finally a post..

Quote:
I just don't know why you have different cache function wich do the same thing. And by the way, if you wrote that as one loop isntead of 100 you wouldn't it would have been a lot better. Now to answer you questions :

Quote:
Originally Posted by Thunder_Eye
I am aware that the code sucks, I will fix that when it works (merging loops etc)

Quote:
1.The store bag and store item id do the same thing - they store an integer. And, they store that integer with the same mission key( I2S(H2I(u)) ) and the same key(I2S(k)). So the second time you store them it overwrites the first.
-Solution : just store all the items at the same time, bags or not. Instead of destroying them just put all of them somewhere.

2. Same cause as 1, thou a different result. When it tries to get a bag it doesn't know if the thing it gets is stored by SetUnitItemId or StoreBag, because they both store an integer with the same keys. It returns the item rawcode as an item pointer and when the functions attemts to return that item the game crashes.
-Solution : same as 1. Give back the items you put away earlier.

And please, Use a single loop damn it.

ah right forgot about them overwriting, will test it tomorrow and see if I get another result
08-27-2006, 08:59 PM#4
RodOfNOD
function Trig_Back0_Actions takes nothing returns nothing
local item i = GetManipulatedItem()
local item j = null
local unit u = GetManipulatingUnit()
local integer k = 0

//Clean Unit
call FlushHandleLocals(u)
//Attach the opened bag to unit
call SetUnitCurrentBag(u, "current", i)
//Attach possible bags in slot 1-6 to the unit
loop
exitwhen k == 6
if GetItemTypeId(UnitItemInSlot(u, k)) == 'I000' then
call SetUnitBag(u, I2S(k), UnitItemInSlot(u, k))
endif
set k = k + 1
endloop
set k = 0
//Move Bags from unit
loop
exitwhen k == 6
call SetItemPosition(GetUnitBag(u,I2S(k)), 100, 100)
set k = k + 1
endloop
set k = 0
//Save items id's on the unit in slot 1-6
loop
exitwhen k == 6
call SetUnitItemId(u, I2S(k), GetItemTypeId(UnitItemInSlot(u, k)))
set k = k + 1
endloop
set k = 0
//Remove items in slot 1-6
loop
exitwhen k == 6
call RemoveItem(UnitItemInSlot(u, k))
set k = k + 1
endloop
//=======================LOAD ITEMS FROM BAG=======================
set k = 1
//Create "Back" in slot 1
set j = UnitAddItemById(u, 'I001')
call UnitDropItemSlot(u, j, 0)
//Create items from bags slot 2-6 and set their correct position
loop
exitwhen k == 6
set j = UnitAddItemById(u, GetBagItemId(i, I2S(k)))
call UnitDropItemSlot(u, j, k)
set k = k + 1
endloop
endfunction


function Trig_Back1_Actions takes nothing returns nothing
local unit u = GetManipulatingUnit()
local item i = GetManipulatedItem()
local item a = GetUnitCurrentBag(u, "current")
local item it = null
local integer k = 0

call FlushHandleLocals(a)
loop
exitwhen k == 6
if GetItemTypeId(UnitItemInSlot(u, k)) == 'I000' then
call SimError(GetOwningPlayer(u),"Cannot close backpack")
return
endif
set k = k + 1
endloop
set k = 1
loop
exitwhen k == 6
call SetBagItemId(a, I2S(k), GetItemTypeId(UnitItemInSlot(u, k)))
set k = k + 1
endloop
set k = 0
loop
exitwhen k == 6
set j = UnitItemInSlot(u, k)
call RemoveItem(j)
set k = k + 1
endloop
set k = 0
loop
exitwhen k == 6
set j = GetUnitBag(u, I2S(k))
call UnitAddItem(u, j)
call UnitDropItemSlot(u, j, k)
set k = k + 1
endloop
set k = 0
loop
exitwhen k == 6
set it = UnitAddItemById(u, GetUnitItemId(u, I2S(k)))
call UnitDropItemSlot(u, it, k)
set k = k + 1
endloop
endfunction

function Trig_Backpack_Actions takes nothing returns nothing
if GetItemTypeId(GetManipulatedItem()) == 'I000' then
call Trig_Back0_Actions()
elseif GetItemTypeId(GetManipulatedItem()) == 'I001' then
call Trig_Back1_Actions()
endif
endfunction

//===========================================================================
function InitTrig_Backpack1 takes nothing returns nothing
local trigger t = null

set t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_USE_ITEM )
call TriggerAddAction( t, function Trig_Back1_Actions)

set t = null
endfunction