Game Development Community

How to save the players progress?

by Andre Zangari · in Torque Game Engine · 06/21/2005 (6:42 am) · 16 replies

Hi,

I haven't found yet in Torque a way to create/open a file to save the players progress (ex: to save in hd the maximum number of points he scored when he shuts down his PC)
Could anyone help me? Thanks!

#1
06/21/2005 (7:15 am)
You could just store the vairiables you need to recall to an external data file and then recall them when the game is restarted.

That's what I'd do anyway.
#2
06/21/2005 (7:34 am)
Thanks. I thought about doing that. But how would be the code to do it in Torque? I have no ideia
#3
07/06/2005 (7:27 am)
I'm also interested in opening and saving files via torque.

Are there functions comparable to the "filestream-functions" from c++ und delphi?
#4
07/06/2005 (7:52 am)
Here is the code to write straight to a file, assuming "$game::score" is set to something..

$game::score = 1000;
function saveGame()
{

   %filePath = "starter.fps/data/saves/gameSave.cs";
   %file = new FileObject();

   if(%file.openForWrite(%filePath))
   {
      %file.writeLine("$game::score = " @ $game::score @ ";");
   } else
   {
      error("Could not open the file to WRITE at the path -" SPC %filePath SPC "!");
   }

   %file.close();
   %file.delete();

}

since we set it up as a .cs file and saved out the value like this "$game::score = 1000;" we can exec() it like a script file to load the data, like this.

function loadSave()
{
   %filePath = "starter.fps/data/saves/gameSave.cs";

   exec(%filePath);
}


if we wanted to read the file like we would write it we could do this to read through each line and eval() each line.

function loadGame()
{
   %filePath = "starter.fps/data/saves/gameSave.cs";
   %file = new FileObject();

   if(%file.openForRead("starter.fps/data/saves/gameSave.cs"))
   {
      while(!%file.isEOF())
      {
         %line = %file.readLine();
         eval(%line);
      }
   } else
   {
      error("Could not open the file to READ at the path -" SPC %filePath SPC "!");
   }
   
   
   %file.close();
   %file.delete();

}
#5
07/06/2005 (7:57 am)
That's great!

I'll try it immediately, when i've solved my problem with refreshing the gui: Link
#6
07/06/2005 (8:10 am)
Another way is to attach it to an object... most commonly would be either a SimSet or a ScriptObject

like this

new ScriptObject(gameData);

gameData.score = 1000;

gameData.save("starter.fps/data/saves/gameData.cs");

this will create a script object, set its "score" value to 1000, then save it out to gameData.cs... the file will look like this

//--- OBJECT WRITE BEGIN ---
new ScriptObject(gameData) {
      score = "1000";
};
//--- OBJECT WRITE END ---

then you can exec() the file like this

exec("starter.fps/data/saves/gameData.cs");

and can access the script object now

echo(gameData.score);

SimSets will hold objects, so you could create one like this

new SimSet(game);

then add gameData to it

game.add(gameData);

then save out the game SimSet like this

game.save("starter.fps/data/saves/game.cs");

the outputed save file will look like this

//--- OBJECT WRITE BEGIN ---
new SimSet(Game) {

   new ScriptObject(gameData) {
         score = "1000";
   };
};
//--- OBJECT WRITE END ---

to load it back in you would use an exec command like this

exec("starter.fps/data/saves/game.cs");
#7
07/06/2005 (8:12 am)
You can iterate through a SimSet like this :)

%count = game.getCount();

for(%i=0;%i<%count;%i++)
{
   %obj = game.getObject(%i);
}
#8
07/06/2005 (8:32 am)
Very nice. Thanks, Matthew.
#9
07/08/2005 (11:26 am)
Can you delete ScriptObjects from a SimSet?
#10
07/08/2005 (11:32 am)
Yes... heres some commands I did to demonstrate it

new Scriptobject(testSO);
new simset(SS);
SS.add(testSO);
echo(SS.getObject(0));
// The ID is echo'd to the console
SS.remove(SS.getObject(0));
echo(SS.getObject(0));
// -1 is echoed to the console since there is no more object
#11
07/08/2005 (11:36 am)
Thanks, very nice to know :D
I assume the parameter for the getObject function is a number relating to where the object you want to delete is in the list. 0 is the first, 1 is the second etc. is that right?
#12
07/08/2005 (11:38 am)
Correct :)
#13
07/08/2005 (11:46 am)
Heres a function you can add in script to add a "getObjectByName" function to SimSets

function SimSet::getObjectByName(%this, %name)
{
   %count = %this.getCount();

   for(%i=0;%i<%count;%i++)
   {
      %obj = %this.getObject(%i);
      if(%obj.getName() $= %name)
         return %obj;
   }

   return -1;
}

heres how I tested it

new ScriptObject(SO);
new SimSet(SS);
SS.add(SO);
echo(SS.getObjectByName("SO"));

it echoed the ScriptObject's ID :)
#15
11/20/2005 (10:45 am)
I am trying to save to a file ScriptObjects that have other ScriptObjects as member variables. Using the save() function, I am able to write out all the objects and their child objects to a file, but I have been unable to read them back in *and* reestablish the parent-child relationship. This is because, in the file, the member variable slots just point to the number descirbing the child object. And, since the child object does *not* take the same number pointer ("handle") when it is read in, I cannot recreate the relationship.

Any ideas on how to make this work?

Or, any ideas on other ways to write a hierarchy of ScriptObjects to a file, and read them back in?

Thanks in advance! I'll post back here if I figure something out.
#16
11/16/2007 (1:03 pm)
Ok, this is exactly what I needed, I just have tow questions. If you save it as a regular cs file, couldn't player's modify that? So, Is there a way to delete the *.cs file with out getting rid of the *.cs.dso? Also, can you exec a file multiple times, if it gets changed?