Game Development Community

How to save and load that level

by Shunmugam · in Torque Game Builder · 02/17/2010 (6:24 am) · 6 replies

i want to save my game level and load that level., how to save every level status.

#1
02/17/2010 (2:30 pm)
My suggestion is to use the scripted fileIO system to write and read text files. Save all of your game variables such as health, armor, location, etc in a text file when the level is saved then load it back in for a level load.
#2
02/17/2010 (4:43 pm)
I really, really like to make a ScriptObject and put all of my game's state variables in there. Then I can just do the following:

(Note: this code is a bit of an oversimplification of what I do, but it's a good place to start. Also, this code isn't tested, just from memory... coder beware!)
function createNewGameState()
{
  new ScriptObject(GameState)
  {
    stateVariable1 = 0;
    stateVariable2 = 0;
  };
}

function GameState::saveState( %this )
{
  new FileObject( SaveFile );
  SaveFile.openForWrite( "state.obj" );
  SaveFile.writeObject( GameState );
  SaveFile.close();
  SaveFile.delete();
}

function loadGameState()
{
  exec( "state.obj" );
}

As long as you can initialize your game with the "GameState" object, it shouldn't matter whether you create a new one or load it first.
#3
03/07/2010 (8:38 pm)
Thanks for your reply, it,s work perfectly
#4
05/18/2010 (5:00 pm)
Will this approach work:
function GlobalLevel::createNewGameState()  
{  
   new ScriptObject(GameState)  
   {  
      rows=%this.rows;
      cols=%this.cols;
      bonus=$setDiff.bonus;
      map=$setMap.map;
   };
   
   $Player.fillState(GameState);
   
   for (%s=0;%s<%this.sectorCnt;%s++){
      $sector[%s].fillState(GameState);
   }
  
}

And then it each class have a "fillState" method:

function player::fillState(%this, %GameState)
{
   %GameState.playerClass = %this.class;
   %GameState.playerPos = %this.position;
...
}

or am I making this too complicated?

What would be nice is if I could do this:

function GlobalLevel::createNewGameState()  
{  
   new ScriptObject(GameState)  
   {  
      rows=%this.rows;
      cols=%this.cols;
      bonus=$setDiff.bonus;
      map=$setMap.map;

      player = $player; //does this capture every obj owned by player?
      sector0 = $sector[0]; //same question
   };
}

I can't imagine its that simple...
#5
05/19/2010 (3:06 pm)
Your first way will work. The second, not so much. You are right in assuming that the classes won't save all of their data.

There is another thread about a year old or so where I showed somebody how Torque saves their levels and they were able to use that to save/load all of the classes. I'd try to find it for you, but I'm on vacation and only had a few seconds to look in.

Good luck!
#6
05/19/2010 (7:29 pm)
Thanks William! I think I'll have to do this the hard way because the objects have to be reconstructed in a certain order. I'm glad I saved this for last...