I made a "include" file for scripts that needs to have there variables saved and loaded when they go out of scope (game reload or outside of the loaded world).
All the variables that needs to be saved must be stored in a global table. The name of the table does not matter.
"SaveVar.nut" must be saved in the games directory, not the script directory.
// SaveVar.nut
NeedLoad <- true;
Pos <- [0,0,0];
function Dump(var)
{
local i,v;
local s="";
if (typeof var=="null") {
s = "null";
} else if (typeof var=="string") {
s = "\""+var+"\"";
} else if (typeof var=="array") {
s = "[";
foreach (v in var) {
s += Dump(v)+",";
}
s += "]";
} else if (typeof var=="table") {
s = "{";
foreach (i,v in var) {
if (typeof i=="integer") {
s += "["+i+"]="+Dump(v)+",";
} else {
s += i+"="+Dump(v)+",";
}
}
s += "}";
} else { // integer bool ??
s = var+"";
}
return s;
}
function SaveVar(varname)
{
local c,s,filename;
s="";
filename = format("%d_%d_%d.save",Pos[0],Pos[1],Pos[2]);
if (varname in getroottable() && typeof varname="string") {
if (typeof (getroottable()[varname])=="table") {
foreach (i,v in getroottable()[varname]) {
if (typeof i=="integer") {
s += "::"+varname+"["+i+"]<-"+Dump(v)+";\n";
} else {
s += "::"+varname+"."+i+"<-"+Dump(v)+";\n";
}
}
} else {
s = "::"+varname+"<-"+Dump(getroottable()[varname])+";\n";
}
c = compilestring(s);
writeclosuretofile(filename,c);
}
}
function LoadVar()
{
local s;
s = format("%d_%d_%d.save",GetX(),GetY(),GetZ());
NeedLoad = false;
try {
dofile(s);
} catch(e) {
return false;
}
try {
remove(s);
} catch(e) {}
return true;
}
function SavePos()
{
Pos[0]=GetX();
Pos[1]=GetY();
Pos[2]=GetZ();
}
result = LoadVar()
Loads the vars for this script.
Result is true if there where variables loaded, otherwise false.
SaveVar( tablename )
Save the variables in the global table named tablename. tablename is the name of the table, not the table it self.
Ex: SaveVar("Persistent");
SavePos()
Stores the robots position for use by SaveVar in Voxel_Unload() since we don't have access to the robots position in Voxel_Unload(). Needs to be called after every time we have moved.
The variables are stored as X_Y_Z.save in a compiled Squirrel script in the games directory. If you have the game installed in "/Program Files" you will need to give your self write access to the game directory, or it will not work.
WARNING! This script does not know what Universe you are in so problems may occur if you play several universes.
Example script that uses SaveVar.nut for saving and loading variables
//global table with the variables that the script needs to be saved and reloaded
Persistent <- {
StartX = 0, // The X pos we started at
StartZ = 0, // The Z pos we started at
BlockType = 0, // The type of Block we are on top of
}
function Voxel_Load()
{
// "Include" SaveVar.nut
dofile("SaveVar.nut");
}
function Voxel_Unload()
{
// Save Variables
SaveVar("Persistent"); // Important, use the name of the global table, not the table it self.
}
function Voxel_Step()
{
// Load our variables if needed
if (NeedLoad)
LoadVar();
// Do stuff
Display("StartX="+Persistent.StartX+" StartZ="+Persistent.StartZ+" BlockType="+Persistent.BlockType, 2000, 4, 1000);
// Last thing in the Voxel_Step
SavePos();
}
Edit: Oops. Forgot to support "null", "true" and "false". Updated SaveVar.nut