Blockland Forums > Modification Help
Saving And Loading Vars
jes00:
--- Quote from: MegaScientifical on December 08, 2011, 05:06:12 PM ---What makes you think exporting the variables removes them from the games memory? They're still there, you don't need to reload them.
--- End quote ---
Ok, lets say there is a little noob named Bob who joins the server and gets ten gold, Bob now leaves the server and comes back, now the server needs to execute the file holder everyone's vars or Bob will have no gold and he will say "What?!?! what is my gold?". The End.
Superb:
This is just a little sample using file objects. The save variables and the load variables inside of the save function and load function must be in the same order (when using this simple method). Notice how we use the packaged functions to save and load clients.
function saveClient(%this)
{
%f = new fileObject();
%f.openForWrite("filepath"@%this.bl_id@".txt");
%f.writeLine(%this.gold);//1
%f.writeLine(%this.level);//2
%f.close();
%f.delete();
}
function loadClient(%this)
{
%f = new fileObject();
%f.openForRead("filepath"@%this.bl_id@".txt");
%this.gold = %f.readLine();//1
%this.level = %f.readLine();//2
%f.close();
%f.delete();
}
//start of the it needs to be in a package package
function gameConnection::autoAdminCheck(%this)
{
parent::autoAdminCheck(%this);
//when someone joins, let's load them up!
loadClient(%this);
}
function gameConnection::onClientLeaveGame(%this)
{
//when someone leaves, let's save them before we call the parent!
saveClient(%this);
parent::gameConnection::onClientLeaveGame(%this);
}
//end of the needs to be in a package package
jes00:
--- Quote from: Superb on December 08, 2011, 05:38:45 PM ---This is just a little sample using file objects. The save variables and the load variables inside of the save function and load function must be in the same order (when using this simple method). Notice how we use the packaged functions to save and load clients.
function saveClient(%this)
{
%f = new fileObject();
%f.openForWrite("filepath"@%this.bl_id@".txt");
%f.writeLine(%this.gold);//1
%f.writeLine(%this.level);//2
%f.close();
%f.delete();
}
function loadClient(%this)
{
%f = new fileObject();
%f.openForRead("filepath"@%this.bl_id@".txt");
%this.gold = %f.readLine();//1
%this.level = %f.readLine();//2
%f.close();
%f.delete();
}
//start of the it needs to be in a package package
function gameConnection::autoAdminCheck(%this)
{
parent::autoAdminCheck(%this);
//when someone joins, let's load them up!
loadClient(%this);
}
function gameConnection::onClientLeaveGame(%this)
{
//when someone leaves, let's save them before we call the parent!
saveClient(%this);
parent::gameConnection::onClientLeaveGame(%this);
}
//end of the needs to be in a package package
--- End quote ---
I know nothing about file objects so what does this do exactly?
Superb:
--- Quote from: jes00 on December 08, 2011, 05:44:35 PM ---I know nothing about file objects so what does this do exactly?
--- End quote ---
LOOK at it. If that's not enough, copy and paste it somewhere and experiment with it lol.
jes00:
--- Quote from: Superb on December 08, 2011, 05:52:09 PM ---LOOK at it. If that's not enough, copy and paste it somewhere and experiment with it lol.
--- End quote ---
Tested(and I fixed the parent). Effect: nothing.