Game Development Community

Restarting a level

by Konrad Carstein · in Torque Game Builder · 06/28/2008 (11:47 pm) · 3 replies

Hi all, me again. You probably see me all over this forum asking a ton of questions. But thank you to all who have responded and continue to do so.

I've been great advances in my project this last week and if I had a schedule I would be ahead of it I'm sure. :)

One of my latest challenges is the following:

Upon the players death I need to do some things but then ultimately restart the level.

Question 1
This code always causes a crash. No script errors, just a big "No longer responding" by windows and it shuts down. What have I done wrong? It seems to work when called from game.cs but if I do it anywhere else, its toast.

sceneWindow2D.loadLevel("game/data/levels/level1.t2d");


Question 2
What is the best means of storing which level the player is on so that you could reload the correct level? I'm sure there are some means like reading it from a file using IO, but are there ways to do it so that a player with a text editor couldn't simply jump themselves to the end level of the game by changing the saved level?

Thanks again!

#1
06/29/2008 (12:18 am)
Question 1:

Try doing this:

function loadLevel(%levelFile)
{
    schedule(100, sceneWindow2D, "loadLevel", %levelFile;
}

This little problem relates to what happens when an object is destroyed. I imagine what you've done is call the load level function from an object that gets deleted upon loading the level again. Give it a whirl and see what happens.

Question 2:

I suggest that you locate the function "t2dSceneWindow::loadLevel(%sceneWindow, %levelFile)" inside of your /common/gameScripts/levelManagement.cs. I would throw this at the top:

$LastLoadedLevel = %levelFile;

This way you always have the file name handy.

Hope that helped ;)
#2
06/29/2008 (12:26 am)
Using the schedule function to load the level seems to work the first time, but not twice in a row. I basically, for the time being just want to reload the level when my player dies. Using the code you included the above works wonderfully on the first death. If I let him die a 2nd time, it crashes with a runtime error and shutdown.

Thanks for the $LastLoadedLevel, thats a great idea. However what if I wanted for it to persist even after the game was shutdown? Like a save game? Are there any resources or further information on that type of mechanic?

Thanks again.
#3
06/29/2008 (11:37 am)
Unstable,

Here's a restart function that I bound to f6 and use quite frequently for beta testing:

function restart()
{
   endGame();
   schedule(200, 0, "startGame", expandFilename($Game::DefaultScene));  
}

Question 2:

You could save it to a text file like you said. Here's some basic code I wrote recently, though you'll obviously have to adapt it a little:

Saving:
function player::saveProfile(%this)
{
   warn("Game saved, dude.");
   %filename = $profileSaveFile;   
            
   if(!isWriteableFileName(%filename))
   {
      error("Unwriteable file path:" SPC %filename);
      return "Unwriteable file path:" SPC %filename;
   }
   
   %fo = new FileObject();
      
   if(!%fo.openForWrite(%filename))
   {
      %fo.delete(); 
      error(%filename SPC ":unable to be opened for write.");
      return %filename SPC ":unable to be opened for write.";
   }
   
   %fo.writeLine("[POSITION]" SPC %this.getPosition());
   %fo.close();
   %fo.delete();
}

Loading:
function loadProfile()
{
   warn("Game loaded, dude");
   %filename = $profileSaveFile;
   
   %fo = new FileObject();
      
   if(!%fo.openForRead(%filename))
   {
      %fo.delete(); 
      error(%filename SPC ":unable to be opened for read.");
      return %filename SPC ":unable to be opened for read.";
   }
   
   while(!%fo.isEOF())
   {
     %line = %fo.readLine();
                 
      if(getWord(%line, 0) $= "[POSITION]")
         $playerRestartPosition = getWord(%line, 1) SPC getWord(%line, 2);
   }
   
   %fo.close();
   %fo.delete();
}

Here's the filename under which I save the file:
$profileSaveFile = "data/save/profile1.txt";
That of course appears in app data/company name/project name/data/save . . .

You could create a coded system where the load function interprets the level based on what combo of characters and numbers you use.

ie

e23jjah23 = level5.t2d

Or you could encrypt the text file if you had the pro version.

[edit]

Here's a link to text file encryption dll posted here on GG:

www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=14715