Updated the "include" file for saving variables to use the new features in v1.22b3.
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" now goes into the Universes\nr\Scripts\Squirrel\ directory.
// SaveVar.nut
SaveName <- null;
function MakeSaveName()
{
if (GetPath(4)!=-1) {
SaveName = format("%s%s%d_%d.save",GetPath(4),GetInfo(20),GetInfo(22),GetRobotID());
} else {
throw "Needs to be called from Voxel_Step()";
}
}
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;
if (SaveName == null)
throw "Need to call LoadVar() or MakeSaveName() from Voxel_Step() before calling SaveVar()";
s="";
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";
}
error(s);
c = compilestring(s);
writeclosuretofile(SaveName,c);
}
}
function LoadVar()
{
if (SaveName==null)
MakeSaveName();
try {
dofile(SaveName,false);
} catch(e) {
return false;
}
/* Remove comment to have the save file removed on load
try {
remove(s);
} catch(e) {}
*/
return true;
}
result = LoadVar()
Loads the vars for this script. Must be called from Voxel_Step since the GetPath and GetInfo is not available in Voxel_Load.
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.
You need to call LoadVar() or MakeSaveName() from Voxel_Step before a call to SaveVar() can be made in Voxel_Unload.
Ex: SaveVar("Persistent");
The variables are stored as ScriptNr_robotID.save in a compiled Squirrel script in Universes\nr\Scripts\UserData directory.
Example script that uses SaveVar.nut for saving and loading variables
NeedInit <- true;
//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_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 (NeedInit)
NeedInit = false; // Need to take care of the Init our self now
// Include SaveVar.nut
dofile(GetPath(3)+GetInfo(20)+"SaveVar.nut",true);
// Load our variables
LoadVar();
}
// Do stuff
Display("StartX="+Persistent.StartX+" StartZ="+Persistent.StartZ+" BlockType="+Persistent.BlockType, 2000, 4, 1000);
}