TriggerHappy
...
- Reaction score
- 341
SimCache
TriggerHappy187
TriggerHappy187
Introduction
I never really expected anything out of this, I was just messing around trying to simulate game cache.
Turns out S2ID returns low enough integers that can be stored by struct indexes.
And by combining the missionKey, a divider and the key into S2ID(missionKey + "$|#%&" + key)
it produces a unique value (seemingly, unless someone can prove this wrong)
Thats all there really is to it, i'm waiting for someone to prove this wrong as I am still in disbelief that I simulated game cache by adding two values.
Here is an example usage..
JASS:
scope SimCacheTest initializer InitTrig
function Actions takes nothing returns nothing
local integer i
call CacheInteger("test", "ing", 10)
call CacheInteger("a", "", 15)
set i = RestoreInteger("a", "") + RestoreInteger("test", "ing")
call BJDebugMsg(I2S(i)) // this will display 25.
endfunction
//===========================================================================
function InitTrig takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterTimerEventSingle( t, 0.00 )
call TriggerAddAction( t, function Actions )
endfunction
endscope
JASS:
//***************************************************************************
//*
//* SimCache - By TriggerHappy187
//*
//***************************************************************************
//*
//* Installation
//* All you need to do is copy this script into your map and you can use
//* the provided functions.
//*
//***************************************************************************
//*
//* Requirements
//* This requires no external systems, all it requires is JassHelper.
//*
//***************************************************************************
//*
//* Limitations
//* if your strings id is greater than 16380, then this will bug.
//*
//***************************************************************************
library SimCache
private function S2ID takes string s returns integer
return s
return 0
endfunction
//! textmacro SimCache takes TYPE,HANDLE,DEF
globals
private $HANDLE$ array $HANDLE$_val
private $HANDLE$ array $HANDLE$_val_safe
endglobals
function Cache$TYPE$ takes string missionKey, string key, $HANDLE$ value returns nothing
local integer i = S2ID(missionKey + "$|#%&" + key)
if i < 8191 then
set $HANDLE$_val<i> = value
else
set $HANDLE$_val_safe[i-8190] = value
endif
endfunction
function Restore$TYPE$ takes string missionKey, string key returns $HANDLE$
local integer i = S2ID(missionKey + "$|#%&" + key)
if i > 8190 then
return $HANDLE$_val_safe[i-8190]
endif
return $HANDLE$_val<i>
endfunction
function Flush$TYPE$ takes string missionKey, string key returns nothing
local $HANDLE$ def = $DEF$
set $HANDLE$_val[S2ID(missionKey + "$|#%&" + key)] = def
endfunction
//! endtextmacro
//! runtextmacro SimCache("Integer","integer","0")
//! runtextmacro SimCache("Real","real","0.")
//! runtextmacro SimCache("Boolean","boolean","false")
//! runtextmacro SimCache("String","string","null")
endlibrary</i></i>