Game Development Community

Saving/loading in T2D example

by Charlie Malbaurn · in Torque Game Builder · 04/21/2005 (6:15 am) · 10 replies

Ok, so here is a start over on how to save and load files. This has been more thought out then the last one. That one was thrown together and not so clear.

First thing we are going to do is create a new file and name it savetry.cs and save it in the client folder.Then we are going to add the following 3 functions to it:

function MakeValues()
{
%obj = new ScriptObject(player);
%obj.Something=2;
%obj.somethingelse=4;
player.save("./T2D/client/something.cs");

}

function PrintValues()
{
echo(player.Something);
echo(player.Somethingelse);
}

function SaveNewvalues()
{
player.save("./T2D/client/something.cs");
}

The function makevalues() willl make a new scriptobject named player. Then it will assign 2 and 4 to a couple of variables that I created for it.

The function PrintValues() will just do a little echo to verify that those are infact asigned values
.
The function Savenewvalues() will save new values that we will take care of later.

The first thing we are going to do is open up client .cs in the T2D/client folder. Then we are going to execute something.cs and the not yet existing something.cs file. like this:

exec("./datablocks.cs");
	exec("./savetry.cs");
	exec("./something.cs");

I added it right after datablocks.cs file for the sake of keeping it easy. Even though something.cs doesn't exist yet, it will the next time we run the program so I just added it now.

Now open your copy of T2D.exe. In the console window type:

MakeValues();

Like I said above, this will create a object and assign some stuff to it. Then enter the following code on the console window.

PrintValues();

This will verify by printing 2 and 4

Great, now we are rolling.

Now in the console window type:

player.something=4;

Then

player.somethingelse=8;

Ok cool, so now lets check to see if they stick. In the console window type:

PrintValues();

This will show you that 4 and 8 have been asigned correctly.

Ok now in the console window type:
SaveNewvalues();

This is going to save our new object and variables to a file that we will check in a minute. Now lets close T2D for a sec.

Ok, if you go open /T2D/client/something.cs with notepad or whatever, you should see that the values are 4 and 8.

Great! Now let's see if they saved! Open T2D again. Remember that we put the exec() statement for our at the time non-existent somthing.cs file. Well now that it exists it should read fine.

Also remember that we did not create player anywhere in code. So it is getting it's information straight from something.cs

Ok now open the console window and type:
PrintValues();
it should now say 4 and 8!

That's one way we figure out how to load saved values. Hope it helps

#1
04/29/2005 (8:52 pm)
Hey cool, I was scratching my head about loading and saving stuff, I have to try this out.
#2
04/30/2005 (9:18 am)
Hope it worked for ya
#3
04/30/2005 (10:17 am)
Using this method I added some new functions

fxSceneObject2D::imageMap(%this, %imageMap)
{
      %this.imageMap = %imageMap;
      %this.setImageMap(%imageMap);
}

setting up some functions like these and using them instead adds the .imageMap property to your object... I use a similar function for position and size...

I do this in my Gui Creator so when I save out it saves this information since it doesn't by default on a T2D object... then in my load function I have it reset this info (among other things)
#4
04/30/2005 (11:21 am)
Not to hijack or derail or anything, but this is from the technical overview:
Quote:-Scene saving a loading! The entire scene state can be serialized and saved at any time. Likewise, saved scene data can be loaded, and simulation can pick up right where it left off. Or, you can load a scene, pause time, and re-set any properties of the scene or its objects that you like.
Would that achieve what you guys need? And I wonder how you invoke it?

Also, this sounds like a real-time game editor waiting to happen. :)
#5
04/30/2005 (11:34 am)
That achieves a different goal. For example, if you have a network game players may enter and leave at any time. Serializing the whole scene just isn't appropriate in such a situation.
#6
04/30/2005 (11:37 am)
There is a lot of reasons to have individual values saved.

Right now I am writing a tutorial that will create a high score window in the shooter demo that will highlight why it is good to save information individually.

I'll post it when I am done.
#7
05/25/2005 (8:56 pm)
It's looks good! But anyone can modify "something.cs" that maybe occur unexpected result.
Can we save/Load data with binary values? Thanks!
#8
05/26/2005 (1:26 am)
There are a couple of encryption resources on the site, so you could just take one of those, modify it a little and use that to encrypt your save files.
#9
05/26/2005 (8:38 am)
What you 'could' do is save out the "something.cs" ... exec(); it in the script, th en rewrite it with nothing... that way its a .dso binary file... :) but definately not the most secure way, like Philip said theres encryption resources on the site, do a quick search... there is also this which is an alternative way to write out files
#10
05/27/2005 (2:07 pm)
The first time you write something.cs it changes it to a dso file. If I'm not mistaken you can drop the .cs file and it will just work with new .dso file.

I can't remember exactly. I just built a new computer so all my old code is all over the place.