Author Topic: Saving And Loading Vars  (Read 2335 times)

What makes you think exporting the variables removes them from the games memory? They're still there, you don't need to reload them.
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.

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

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

I know nothing about file objects so what does this do exactly?

I know nothing about file objects so what does this do exactly?

LOOK at it. If that's not enough, copy and paste it somewhere and experiment with it lol.

LOOK at it. If that's not enough, copy and paste it somewhere and experiment with it lol.
Tested(and I fixed the parent). Effect: nothing.

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.

You're still assuming that suddenly the variable no longer exists because he left. If you're saving them as $ variables in the first place, it's still there.

Realizing you want to use the %client.gold, you can still just have it do %client.gold = $jes::blid[%client.bl_id]::gold; or something. Otherwise, it would be simple to just keep it applied to a variable... Meh. I don't think I'll try here anymore, I'm not helping.
« Last Edit: December 08, 2011, 11:37:15 PM by MegaScientifical »

I know nothing about file objects so what does this do exactly?
replace filepath with "config/mydata/"

You're still assuming that suddenly the variable no longer exists because he left. If you're saving them as $ variables in the first place, it's still there.

Realizing you want to use the %client.gold, you can still just have it do %client.gold = $jes::blid[%client.bl_id]::gold; or something. Otherwise, it would be simple to just keep it applied to a variable...
So the variable is not deleted when they leave?
Meh. I don't think I'll try here anymore, I'm not helping.
You've helped the most D:
replace filepath with "config/mydata/"
Result: nothing.

So the variable is not deleted when they leave?You've helped the most D:Result: nothing.

Wow, you are making this 10x harder than it should be. Spoon feeding time.

« Last Edit: December 09, 2011, 07:55:12 AM by Superb »