Game Development Community

How to save player's last position when the Game is Over?

by Skanda · in Torque Game Engine · 10/26/2005 (3:13 am) · 6 replies

As the title.
These days, i thought could i save the player's last position when the Game is over just like RPG Games,then when i loaded in the game again,my player is spawned the last saveing position!

I test this way:

function GameConnection::SaveCharStats(%this)
{
   %tempTrans = %this.player.getTransform();
   %startPostion = getwords(%tempTrans, 0, 2);
   
   // --- Saving and Loading with Torque  Pat Wilson (Nov 08, 2004) 
   // --- Using this method to save player's pos.
   $ioObject = new LoadSaveObject();
   $ioObject.someString = %startPostion;
   $ioObject.saveToFile( "~/saves/players.save" );
   
   echo("\c1 Character Position = " SPC %startPostion);
   return(%startPostion);
}

I put this function in the server/scripts/game.cs,

function GameConnection::onClientLeaveGame(%this)
{

    %this.SaveCharStats();
   ...
}

but it doesn't work!

Could someone help me?
I have no ideas.
Thanks!

#1
10/27/2005 (7:02 am)
Did you implement the LoadSaveObject class in C++? AFAIK, such class doens't exist in TGE. The errors might be output on the console.

Try replacing:

$ioObject = new LoadSaveObject();

By

$ioObject = new ScriptObject();
#3
10/28/2005 (12:31 am)
Thanks,Manoel and Michael !
But I have merged this resources in my TGE's code(Saving and Loading with Torque,Pat Wilson).
The problem is that when i put "SaveCharStats" in server/scripts/game.cs,such as:

function GameConnection::onClientLeaveGame(%this)
{
   // --- New
   %this.SaveCharStats();
   // --- New


   if (isObject(%this.camera))
      %this.camera.delete();
   if (isObject(%this.player))
      %this.player.delete(); // "%this.player" is destoryed at this time.It's that OK?
}

Torque gave me this error::
.../server/scripts/game.cs (331): Unable to find object: '0' attempting to call function 'getTransform'

It seems that "%this.player.getTransform();" has not excuted!
But the "%this.player" is destoryed at the last sentence in that function(%this.player.delete();). so I don't know why this happens?
#4
10/28/2005 (12:51 am)
You're echoing out the position in the first function, what does it say in the console?
Does the position data there relate to where you're standing, or does it not?
#5
10/28/2005 (3:32 am)
The console's output:
.../server/scripts/game.cs (329): Unable to find object: '0' attempting to call function 'getTransform'
Character Position = 1601

It's not OK!
#6
10/28/2005 (3:40 am)
Today, I test the player saving in this way,it seems OK:
1) server/scripts/commands.cs
Add:

function serverCmdSaveCharPos(%client)
{
   %tempTrans = %client.player.getTransform();
   
   %ioObject = new LoadSaveObject();
   %ioObject.someString = getwords(%tempTrans, 0, 2);
   %ioObject.saveToFile( "~/saves/players.save" );        
}

2) client\scripts\ default.bind.cs
Add:
moveMap.bindCmd(keyboard, "x", "commandToServer('SaveCharPos');", "");

3) server\scripts\game.cs
Add:
function getCharPos()
{
   %ioObj = new LoadSaveObject();
   %ioObj.loadFromFile( "~/saves/players.save" );
   echo( %ioObj.someString );
   return(%ioObj.someString);   
}

Change:
function GameConnection::spawnPlayer(%this)
{
   // --- 1 Combination create player and drop him somewhere
   //%spawnPoint = pickSpawnPoint();
   //%this.createPlayer(%spawnPoint);

   // --- [b]New: Get the Saving Player's Pos[/b]
   %spawnPoint = getCharPos();
   %this.createPlayer(%spawnPoint);   
}

in this way,you can press "x" to save player's pos,when you quit game,next time when you enter game,your player is at this spot you just saving!

4) and if you want auto saving player's pos, you can
change function "escapeFromGame" in "client\scripts\default.bind.cs" to:
function escapeFromGame()
{
   if ( $Server::ServerType $= "SinglePlayer" )
      MessageBoxYesNo( "Quit Mission", "Exit from this Mission?", "disconnect();", "");
   else
      MessageBoxYesNo( "Disconnect", "Disconnect from the server?", "disconnect();", "");
      commandToServer('SaveCharPos');   // --- 2005.10.28 new for auto saving
}

So when you quit game,player's pos is autosaving!