Saving and Loading with Torque
by Pat Wilson · 11/08/2004 (8:27 am) · 39 comments
Download Code File
A lot of people ask if Torque can save and load games. The answer is yes, and no. No you can't just say "Torque, save my game!" any more than you can say, "Torque, read my mind and make this awesome MMORPG that I'm sure NOBODY has thought of before." You need to tell Torque what data you want to save, and in what order you want to save it. The code is attached, if you want to play with it, just compile it in to Torque and it will work.
First, the object
Note that there are some data members in there, we will be saving that data, in the functions 'saveToFile' and 'loadFromFile'.
Without further delay, the save function
Note the order in which we wrote things in to the file, we need to read them out in exactly the same order in the read function.
You now have a object you can mess with from script that will save and load. Here is a little script you can use to test it out.
Now go forth and make something!
A lot of people ask if Torque can save and load games. The answer is yes, and no. No you can't just say "Torque, save my game!" any more than you can say, "Torque, read my mind and make this awesome MMORPG that I'm sure NOBODY has thought of before." You need to tell Torque what data you want to save, and in what order you want to save it. The code is attached, if you want to play with it, just compile it in to Torque and it will work.
First, the object
class LoadSaveObject : public SimObject
{
typedef SimObject Parent;
protected:
S32 mSomeS32;
StringTableEntry mSomeString;
F32 mSomeF32;
public:
LoadSaveObject() : mSomeS32(0), mSomeString( "" ), mSomeF32( 0.f ) {};
static void initPersistFields();
bool saveToFile( const char *filename ) const;
bool loadFromFile( const char *filename );
DECLARE_CONOBJECT(LoadSaveObject);
};Note that there are some data members in there, we will be saving that data, in the functions 'saveToFile' and 'loadFromFile'.
Without further delay, the save function
bool LoadSaveObject::saveToFile( const char *filename ) const
{
char fname[1024];
if( filename )
if( Con::expandScriptFilename( fname, sizeof( fname ), filename ) )
filename = fname;
FileStream fs;
if( !fs.open( filename, FileStream::Write ) )
{
Con::errorf( "LoadSaveObject: Could not open '%s' for writing.", filename );
return false;
}
fs.write( mSomeS32 );
fs.writeString( mSomeString, dStrlen( mSomeString ) );
fs.write( mSomeF32 );
fs.close();
return true;
}Note the order in which we wrote things in to the file, we need to read them out in exactly the same order in the read function.
bool LoadSaveObject::loadFromFile( const char *filename )
{
char fname[1024];
if( filename )
if( Con::expandScriptFilename( fname, sizeof( fname ), filename ) )
filename = fname;
FileStream fs;
if( !fs.open( filename, FileStream::Read ) )
{
Con::errorf( "LoadSaveObject: Could not open '%s' for reading.", filename );
return false;
}
fs.read( &mSomeS32 );
char buffer[256];
fs.readString( buffer );
mSomeString = StringTable->insert( buffer );
fs.read( &mSomeF32 );
fs.close();
return true;
}You now have a object you can mess with from script that will save and load. Here is a little script you can use to test it out.
$foo = new LoadSaveObject(); $foo.someS32 = -42; $foo.someString = "tEsTING caSE SEnSITIvE STRingS!"; $foo.someF32 = 3.14159; $foo.saveToFile( "~/saved.foo" ); $bar = new LoadSaveObject(); $bar.loadFromFile( "~/saved.foo" ); echo( $bar.someS32 ); echo( $bar.someString ); echo( $bar.someF32 );
Now go forth and make something!
About the author
#3
11/08/2004 (8:46 am)
It's not working for me either.
#4
11/08/2004 (9:55 am)
Please re-upload your attachment.
#5
when you come back and load your "saved game" will you still only have 10 ai to finish off?
11/09/2004 (5:29 am)
will this save a current map, as in, say you start a map with 20 ai in it, you blast your way through 10 of the ai, your wife hollers "we need a loaf of bread" for dinner, so you "save" and shut down the computer and run to the store.when you come back and load your "saved game" will you still only have 10 ai to finish off?
#6
11/09/2004 (5:24 pm)
No, you have to add in what you want it to save.
#7
YA i know, the "some" what ever stuff is your example, but code snippits that use "BS" code in stead of the reall thing is just shoddy at best
11/12/2004 (5:49 am)
ok so its a basic "save" out line. any chance on an example? Perferably somthing that is actually in torque that can be saved, "someString" isnt in torque at all, so to a lot of people this will be worth less. YA i know, the "some" what ever stuff is your example, but code snippits that use "BS" code in stead of the reall thing is just shoddy at best
#8
I re-uploaded the code, I don't know what happened the first time, sorry guys.
11/12/2004 (1:20 pm)
Ace this is telling you how to save and load data from a file. There is no possible way I can write a resource that will save exactly the data you need in your game for you, and it is unrealistic to expect this.I re-uploaded the code, I don't know what happened the first time, sorry guys.
#9
11/12/2004 (2:01 pm)
um no, i was wondering why you dont use real torque code
#10
What were you expecting exactly?
Did you want Pat to write a save function to save out the demo?
Great resource Pat.
11/12/2004 (2:34 pm)
Er Ace this is a real example and if you run it you will find that it saves out the info in the script. What were you expecting exactly?
Did you want Pat to write a save function to save out the demo?
Great resource Pat.
#11
11/12/2004 (5:19 pm)
ok, just looking at it must be over my head
#12
12/05/2004 (5:25 pm)
I think what Ace might be getting at is, the example above is C++ code ... as opposed to Torque script.
#13
01/22/2005 (8:53 am)
ithink he means, show it actually saving an existing variable or something in torque, not just "somestring" or "someF32", actually show it sace something that exists so he could have a better idea of it.
#14
you know i dont eeven dev torque any more, this just came to my email, and it hissed me off , im not the only one who is not UP to your level. Yes you should write out a save fuction to save the demo cuz to me ( and to probably alot more) this is nothing.
01/23/2005 (1:56 am)
exactly! why post stuff half glassed, examples is always the best, not everyone is as smart as every one. Stuff like this just goes in one ear and out the other, cuz really all it is is theory until you use actual code that actualy works you know i dont eeven dev torque any more, this just came to my email, and it hissed me off , im not the only one who is not UP to your level. Yes you should write out a save fuction to save the demo cuz to me ( and to probably alot more) this is nothing.
#15
Currently I'm nearly finished with a data system...
check out my current plan... presently I have a journal system that is almost completely script it saves and loads journal entries... The basis behind it is very open ended (check out functions to get the idea)... this system makes saving and loading data ~extremely~ easy... though you still need to know what data to save, that really comes out to planning and designing of your game... if you give an example of the game and level of interaction then maybe we can help :)
01/24/2005 (8:42 pm)
you have a point, though with something as complex as saving a game it isn't that simple... a lot of it depends on your game... if its a very very basic game then maybe you just need to save the players current health, ammo, weapons, location, etc... if its world interaction then maybe you need to save more...Currently I'm nearly finished with a data system...
check out my current plan... presently I have a journal system that is almost completely script it saves and loads journal entries... The basis behind it is very open ended (check out functions to get the idea)... this system makes saving and loading data ~extremely~ easy... though you still need to know what data to save, that really comes out to planning and designing of your game... if you give an example of the game and level of interaction then maybe we can help :)
#16
Thanks alot I was looking for this, for acount creation and saving! This will make it super simple to create acounts and do a login system . :)
This will also be good for local server logging and various other things.
As far as a map saving function I have an idea
When said wife hollars for said Sl erm husband to go get said loaf of bread and he presses the save button why not try something like
running the function the map editor uses to save missions and safe it as a new mission file?
Then for load just start that new mission file. That should work shouldnt it?
Thanks alot for the safe function safed me tons of time and hair!
01/26/2005 (12:42 pm)
Woot!Thanks alot I was looking for this, for acount creation and saving! This will make it super simple to create acounts and do a login system . :)
This will also be good for local server logging and various other things.
As far as a map saving function I have an idea
When said wife hollars for said Sl erm husband to go get said loaf of bread and he presses the save button why not try something like
running the function the map editor uses to save missions and safe it as a new mission file?
Then for load just start that new mission file. That should work shouldnt it?
Thanks alot for the safe function safed me tons of time and hair!
#17
Uhh im pretty sure this is torque script NOT C++ I may be mistaken sence I do VB for a living and I am a little rusty on my C++ skillz but Im pretty sure that torque decalres there vars with $ and % for local and global vars.
Either way im going to atempt to break down the script for those who dont know what they are looking at sence alot of poeple are confused, please take pitty on me if i make a mistake.
This is an example of what I will use to create an acount for my MMORPG of courrse first i will have todo checks to see if that acount already exists and everything, but you get the point :) Now on to loading an acount.
Now that the file is loaded you would do an if statement to check the file password with the client sent password and send back a yes or no based on it.
this is just a rough rough lay out of what I am going to do with it. I am simply showing an example of how this could be usefull is all.
01/26/2005 (1:11 pm)
$foo = new LoadSaveObject(); $foo.someS32 = -42; $foo.someString = "tEsTING caSE SEnSITIvE STRingS!"; $foo.someF32 = 3.14159; $foo.saveToFile( "~/saved.foo" ); $bar = new LoadSaveObject(); $bar.loadFromFile( "~/saved.foo" ); echo( $bar.someS32 ); echo( $bar.someString ); echo( $bar.someF32 );
Uhh im pretty sure this is torque script NOT C++ I may be mistaken sence I do VB for a living and I am a little rusty on my C++ skillz but Im pretty sure that torque decalres there vars with $ and % for local and global vars.
Either way im going to atempt to break down the script for those who dont know what they are looking at sence alot of poeple are confused, please take pitty on me if i make a mistake.
$objSave = new LoadSaveObject() // creates the load safe object $ObjSave.username = "bob"; // this readys the title username with the string bob to be written $ObjSave.password = "test"; // This readys the title username with the string test to be written $ObjSave.saveToFile( "~/acounts/bob.aco" ); //Saves the acount file in the acounts folder for later use.
This is an example of what I will use to create an acount for my MMORPG of courrse first i will have todo checks to see if that acount already exists and everything, but you get the point :) Now on to loading an acount.
$objLoad= new LoadSaveOBject() //' create the load object
$objLoad .LoadFromFile("~/acounts/bob.aco"); //load the fileNow that the file is loaded you would do an if statement to check the file password with the client sent password and send back a yes or no based on it.
this is just a rough rough lay out of what I am going to do with it. I am simply showing an example of how this could be usefull is all.
#18
04/15/2005 (8:05 pm)
I agree with Isaac, as far as I know, it should also be possible to use the mission file saving function, although it would have to be run on either the ServerGroup or the MissionCleanup objects, since most of the game specific state information is stored in MissionCleanup. You would also have to manually save any global variables you used, as well as anything else with state information that isn't referenced under MissionCleanup. If you save MissionCleanup, you would also probably need to save the mission name so you could load that first using the normal functions, and then load all the game specific information you saved in a separate file. You would probably want to go through and learn everything that happens in the common code and the game specific code for loading a normal mission before trying to save and load specific game states too.
#19
This would be a good starting point.
for those would desire some less prechewed stuff here is how I would go about saving and loading dead and alive enemys.
When an enemy is destoried you would need to save it to memory (an array I think would be best)
Then when the game is saved save the destoried enemys to the file as well.
Then write code to scan the file and "kill" the enemys that are suppose to be dead.
I know this all vague, but the current game I am working on does not really have saving and loading so I am not really looking to write a resource for it. Perhaps in the future when I have less work and more time
Hope I helped
Isaac
04/16/2005 (11:37 pm)
Hmm, most professional games use safe points or check points for this reason, for starters I would just use a save file to tell the cords of the user when the game is loaded. Then call the code to load the mission and put the player at the cords in the file.This would be a good starting point.
for those would desire some less prechewed stuff here is how I would go about saving and loading dead and alive enemys.
When an enemy is destoried you would need to save it to memory (an array I think would be best)
Then when the game is saved save the destoried enemys to the file as well.
Then write code to scan the file and "kill" the enemys that are suppose to be dead.
I know this all vague, but the current game I am working on does not really have saving and loading so I am not really looking to write a resource for it. Perhaps in the future when I have less work and more time
Hope I helped
Isaac
#20
04/21/2005 (10:55 pm)
Wow! Great resource. 
Torque Owner Lukas Kimme
that will help me