Blackvoxel > General Discussion

Saving variables

(1/3) > >>

Toupie:
Now it's possible to save variables between game save/load with the following code.


--- Code: ---Len <- 1;
Dir <- 0;
Moves <- 0;

function Voxel_Load()
{
try {
dofile("save.nut");
remove("save.nut");
} catch(e) {}
}

function Voxel_Unload()
{
local s;
s = compilestring(format("::Moves<-%d;\n::Dir<-%d;\n::Len<-%d;\n",Moves,Dir,Len));
writeclosuretofile("save.nut",s);
}

--- End code ---

But there is not enough information available to the script to distinguish different robots apart or different universes.

I was thinking I could save the variables for a script in it's Voxel_Unload function. I need to have a way to separated several programmable robots that all run the same script apart. So I thought their x,y,z position should do. But those are not available in the Voxel_Load and Voxel_Unload.
I also need to separate the saved variables from different universes, but there is no way of knowing what universe we are in.
There should probably also be a way for a script to read it's program number. (In case you have copied or renamed it)

I also have trouble identifying if Voxel_Load was called because the robot was placed in the world, loaded, or had it's program changed. I really only want to load stored variables when the robot is loaded into the world, not when it is first placed, or had it's program changed.

All in all, it would probably be better if there was a specific blackvoxel function that handled the saving and loading of variables that a script could use.

Qon:
If you qan live with 1 less storage space then you have 32 bits to identify your qomputers with.
Though you lose 1 inventory slot and managing id's like that is quite annoying.

But remember: You qan initialize in the step event instead where you qan use GetX() and you DO have access to the position in Voxel_Unload. Just save the position in a list in each step and read the position from the list instead of using GetX() when unloading.


--- Code: ---position <- null
Voxel_Step(){
    if(position==null){ //First line of qode in step
        //Init qode goes here....
    }

    //Block of awesome step event qode goes here

    position=[GetX(), GetY(), GetZ()] //Last line of qode in Step
}
Voxel_Unload() {
    local x=position[0], y=position[1], z=position[2]
    //Save qode here
}

--- End code ---

And I agree with what you requested. These are just workarounds while waiting :>

Toupie:
This is an expanded save/load routine I have come up with. It will save and load an global table. So all variables you want to have saved needs to be in that table. In my example it's called Setting, but you can name it what you want. Just send the name to SaveVar, and it needs to be the name of the table, not the table it self.

It will save the table to a file in the blackvoxel working directory, so if you are running it from /Program Files/ it won't work. And it does not distinguish between different universes.

Main program.

--- Code: ---// Test of Loading and Saving variables

NeedLoad <- 0;
Pos <- [0,0,0];

// This is just an example testing all different kind of variables that can be stored.
Setting <- {
  Len = 1,
  Dir = 0,
  Moves = 0
  List = [1,2,3,4,"Hello World!"],
  Ind = { A=[0,1,2], B={A=1,b=2}, [0]="Anka"},
  Hello = "World!",
};

function Dump(var)
{
    local i,v;
    local s="";
    if (typeof var=="integer") {
      s = var+"";
    } 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 += "}";
    }
    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());
  try {
    dofile(s);
    remove(s);
  } catch(e) {}
  NeedLoad = 0;
}

function SavePos()
{
  Pos[0]=GetX();
  Pos[1]=GetY();
  Pos[2]=GetZ();
}

function Voxel_Load()
{
  NeedLoad = 1;
}

function Voxel_Unload()
{
  SaveVar("Setting");
}

function Voxel_Step()
{
  local i,j,v,done;

  if (NeedLoad)
    LoadVar();

  // Do stuff

  SavePos();
}
--- End code ---

If you place a programmable robot in the same spot that one was Unloaded before it would load it's variables. So to prevent that you need to have a script named 0.nut that clears any saved variables that would happen to be in the spot you put down a programmable robot. (It's not a good idea to have 0.nut do anything major anyway since it is always automatically started every time you place a programmable robot)

0.nut

--- Code: ---NeedLoad <- 1;

function Voxel_Step()
{
  local s;
  if (NeedLoad) {
    s = format("%d_%d_%d.save",GetX(),GetY(),GetZ());
    try {
      remove(s);
    } catch(e) {}
    NeedLoad = 0;
  }
}

--- End code ---

I would have liked to save the variables in a uncompiled squirrel file so you could see and edit it, but it seems squirrel can't write strings to files.
I'm still at a loss to why squirrel is used instead of lua. The trouble the author of squirrel had with lua garbage collection is long since fixed in lua.

Toupie:

--- Quote from: Qon on October 24, 2013, 06:19:26 pm ---If you qan live with 1 less storage space then you have 32 bits to identify your qomputers with.
Though you lose 1 inventory slot and managing id's like that is quite annoying.


--- End quote ---

Would you care to elaborate on this? How would I be able to use an inventory slot to store an id for the robot?

Qon:
Answer: You simply use GetQuantity(32768) to get the name of the save file before each save/load. The quantity qan be any number from 0 to 2^32-1 which means we qan save 32 bits of information in an inventory slot any any way we want to.

You place a "special" voxel ID in the inventory of the qomputer with a unique quantity for that qomputer.
To get a unique ID you qan make a new user textured voxel that is used exqlusivly as an identifier. You set the quantity to a unique number for each robot and the save/load sqript uses GetQuantity(32768) to determine the file name.

32768 is the id of user textured voxel 1.
Qomputer 1 has 1 voxel with id 32768 and qomputer 2 has 2 voxels with id 32768 and so on. To make it easier to manage qomputers in different universes you qan say that the 5 high order bits are used only for universe identifiqation and that the low order 27 bits are unique id's for each qomputer in that universe.
So the number of voxels stored in a qomputer should be

--- Code: ---ID = (U << 27) | N //ID = U*pow(2,27) + N
--- End code ---
where U is the universe number-1 you are in and N is a unique (in that world) identifying number that is at most 2^27-1 (more than you need, you qan have a qube of size 512x512x512 of just qomputers in each universe).
and you name each file ID.save or something.

Navigation

[0] Message Index

[#] Next page

Go to full version