- Joined
- Apr 27, 2011
- Messages
- 272
This simple snippet will allow you to easily make save, load, and erase functions for any value types (except codes). And you can manage the vaue you saved with integers.
Requires: a knowledge about TextMacros
here's the snippet:
here's a simple test for it
I will be glad to show some extra usage examples requested.
Thanks to: D4RK G4ND4LF and Nestharus for informing me about speed comparison.
Requires: a knowledge about TextMacros
here's the snippet:
JASS:
library ValueManager
//===========================================================================
// Value Manager version 0.0.0 by Alain.Mark
//
// (Note: BE SURE YOU THAT KNOW WHAT TEXTMACROS ARE BEFORE YOU USE THIS)
//
// -About-
// This snippet is actually a template for making simple save, load, and erase
// functions for values of any type (excluding codes).(more explanations below)
//
// -How to Use-
// //! runtextmacro CREATE("<KEYWORD>","<TYPE>")
// -This textmacro will create the save, load, and erase function that you are going
// going to use.
//
// "KEYWORD" - is any combination of unique alphanumeric chars. This will replace the
// $KEYWORD$ tokens inside the CREATE textmacro and instanceate it for use.
//
// "TYPE" - this is the type of argument/value that will be taken by Save_$KEYWORD$
// and returned by Load_$KEYWORD$.
//
// Save_$KEYWORD$($TYPE$ value)
// -This will save the "value" in an array variable and return a unique
// integer that you can use if you want to load "value" back.
//
// Load_$KEYWORD$(integer acces_id)
// -This will load and return the value pointed by the "acces_id".
// (another direct alternative for Load_$KEYWORD$ is accesing the variable itself)
//
// Queue_$KEYWORD$(integer acces_id)
// -This will not actually clear the value pointed by the "acces_id" but
// prepares its slot to be overwritten instead. You should do this once in a
// while to save some space.
//
//===========================================================================
//! textmacro CREATE takes KEYWORD, TYPE
globals
integer $KEYWORD$_Stocks =-1
integer $KEYWORD$_Stockpiled =-1
integer array $KEYWORD$_Stock_No
$TYPE$ array Stored_$KEYWORD$[8190]
endglobals
//===========================================================================
// This function returns an integer so you can track the value stored in
// the array
function Save_$KEYWORD$ takes $TYPE$ value returns integer
local integer acces_id
if($KEYWORD$_Stockpiled>=0)then
set acces_id=$KEYWORD$_Stock_No[$KEYWORD$_Stockpiled]
set $KEYWORD$_Stock_No[$KEYWORD$_Stockpiled]=0
set $KEYWORD$_Stockpiled=$KEYWORD$_Stockpiled-1
else
set $KEYWORD$_Stocks=$KEYWORD$_Stocks+1
if($KEYWORD$_Stocks>=8190)then
debug call BJDebugMsg("|cffff0000WARNING|r: You are saving too many values, try erasing some!!!")
return 0
endif
set acces_id=$KEYWORD$_Stocks
endif
set Stored_$KEYWORD$[acces_id]=value
return acces_id
endfunction
//===========================================================================
// You can directly get the value itself by accessing the variable, I
// just made this function as an interface for it
function Load_$KEYWORD$ takes integer acces_id returns $TYPE$
return Stored_$KEYWORD$[acces_id]
endfunction
//===========================================================================
function Queue_$KEYWORD$ takes integer acces_id returns integer
set $KEYWORD$_Stockpiled=$KEYWORD$_Stockpiled+1
set $KEYWORD$_Stock_No[$KEYWORD$_Stockpiled]=acces_id
return acces_id
endfunction
//! endtextmacro
//===========================================================================
endlibrary
here's a simple test for it
JASS:
library Test initializer Example
// Let's test it on strings
//! runtextmacro CREATE("Str","string")
// I decided to use "Str" as my KEYWORD hence it will replace $KEYWORD$ tokens the resulting function names I will be
// using are Save_Str, Load_Str, and Erase_Str
private function Example takes nothing returns nothing
local integer a=Save_Str("Hello World")
call BJDebugMsg(Load_Str(a))
endfunction
endlibrary
JASS:
-Changelog-
// Erase_ was replaced by Queue_ because it might cause some confusion
Thanks to: D4RK G4ND4LF and Nestharus for informing me about speed comparison.
Last edited by a moderator: