function H2I takes handle h returns integer
    return h
    return 0
endfunction

function GUIHandleVars takes nothing returns gamecache
    if udg_GUIHandleVars == null then
        call FlushGameCache(InitGameCache("GUIHandleVars.w3v"))
        set udg_GUIHandleVars = InitGameCache("GUIHandleVars.w3v")
    endif
    return udg_GUIHandleVars
endfunction

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

function StoreRealInTimer takes real value, string name, timer subject returns nothing
    if value == 0 then
        call FlushStoredReal(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreReal(GUIHandleVars(), I2S(H2I(subject)), name, value)
    endif
endfunction

function StoreBooleanInTimer takes boolean value, string name, timer subject returns nothing
    if value == false then
        call FlushStoredBoolean(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreBoolean(GUIHandleVars(), I2S(H2I(subject)), name, value)
    endif
endfunction

function StoreUnitInTimer takes unit value, string name, timer subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function StoreItemInTimer takes item value, string name, timer subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function StoreGroupInTimer takes group value, string name, timer subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function StoreEffectInTimer takes effect value, string name, timer subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function StoreLocInTimer takes location value, string name, timer subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function LoadIntFromTimer takes string name, timer subject returns integer
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
endfunction

function LoadRealFromTimer takes string name, timer subject returns real
    return GetStoredReal(GUIHandleVars(), I2S(H2I(subject)), name)
endfunction

function LoadBooleanFromTimer takes string name, timer subject returns boolean
    return GetStoredBoolean(GUIHandleVars(), I2S(H2I(subject)), name)
endfunction

function LoadUnitFromTimer takes string name, timer subject returns unit
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadItemFromTimer takes string name, timer subject returns group
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadGroupFromTimer takes string name, timer subject returns group
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadEffectFromTimer takes string name, timer subject returns effect
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadLocFromTimer takes string name, timer subject returns location
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

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

function StoreRealInUnit takes real value, string name, unit subject returns nothing
    if value == 0 then
        call FlushStoredReal(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreReal(GUIHandleVars(), I2S(H2I(subject)), name, value)
    endif
endfunction

function StoreBooleanInUnit takes boolean value, string name, unit subject returns nothing
    if value == false then
        call FlushStoredBoolean(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreBoolean(GUIHandleVars(), I2S(H2I(subject)), name, value)
    endif
endfunction

function StoreUnitInUnit takes unit value, string name, unit subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function StoreItemInUnit takes item value, string name, unit subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function StoreGroupInUnit takes group value, string name, unit subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function StoreEffectInUnit takes effect value, string name, unit subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function StoreLocInUnit takes location value, string name, unit subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function LoadIntFromUnit takes string name, unit subject returns integer
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
endfunction

function LoadRealFromUnit takes string name, unit subject returns real
    return GetStoredReal(GUIHandleVars(), I2S(H2I(subject)), name)
endfunction

function LoadBooleanFromUnit takes string name, unit subject returns boolean
    return GetStoredBoolean(GUIHandleVars(), I2S(H2I(subject)), name)
endfunction

function LoadUnitFromUnit takes string name, unit subject returns unit
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadItemFromUnit takes string name, unit subject returns group
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadGroupFromUnit takes string name, unit subject returns group
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadEffectFromUnit takes string name, unit subject returns effect
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadLocFromUnit takes string name, unit subject returns location
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

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

function StoreRealInTrigger takes real value, string name, trigger subject returns nothing
    if value == 0 then
        call FlushStoredReal(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreReal(GUIHandleVars(), I2S(H2I(subject)), name, value)
    endif
endfunction

function StoreBooleanInTrigger takes boolean value, string name, trigger subject returns nothing
    if value == false then
        call FlushStoredBoolean(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreBoolean(GUIHandleVars(), I2S(H2I(subject)), name, value)
    endif
endfunction

function StoreTriggerInTrigger takes trigger value, string name, trigger subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function StoreItemInTrigger takes item value, string name, trigger subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function StoreGroupInTrigger takes group value, string name, trigger subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function StoreEffectInTrigger takes effect value, string name, trigger subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function StoreLocInTrigger takes location value, string name, trigger subject returns nothing
    if value == null then
        call FlushStoredInteger(GUIHandleVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(GUIHandleVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function LoadIntFromTrigger takes string name, trigger subject returns integer
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
endfunction

function LoadRealFromTrigger takes string name, trigger subject returns real
    return GetStoredReal(GUIHandleVars(), I2S(H2I(subject)), name)
endfunction

function LoadBooleanFromTrigger takes string name, trigger subject returns boolean
    return GetStoredBoolean(GUIHandleVars(), I2S(H2I(subject)), name)
endfunction

function LoadTriggerFromTrigger takes string name, trigger subject returns trigger
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadItemFromTrigger takes string name, trigger subject returns group
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadGroupFromTrigger takes string name, trigger subject returns group
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadEffectFromTrigger takes string name, trigger subject returns effect
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadLocFromTrigger takes string name, trigger subject returns location
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function LoadTriggerFromTimer takes string name, trigger subject returns trigger
    return GetStoredInteger(GUIHandleVars(), I2S(H2I(subject)), name)
    return null
endfunction

function TimerRunAttachedTrigger takes nothing returns nothing
 local timer t = GetExpiredTimer()
 local trigger tr = LoadTriggerFromTimer("MindWorX Trigger",t)
    call TriggerExecute(tr)
 set t = null
 set tr = null
endfunction

function StartTimerTrigger takes timer t, boolean periodic, real timeout, trigger tr returns nothing
    call StoreInteger(GUIHandleVars(),I2S(H2I(t)),"MindWorX Trigger",H2I(tr))
    call TimerStart(t, timeout, periodic, function TimerRunAttachedTrigger)
endfunction

function TriggerCleanDestroy takes trigger tr returns nothing
    call FlushStoredMission(GUIHandleVars(),I2S(H2I(tr)))
    call DestroyTrigger(tr)
endfunction

function TimerCleanDestroy takes timer t returns nothing
    call FlushStoredMission(GUIHandleVars(),I2S(H2I(t)))
    call DestroyTimer(t)
endfunction

function UnitClean takes unit u returns nothing
    call FlushStoredMission(GUIHandleVars(),I2S(H2I(u)))
endfunction
