Game Development Community

Changing levels

by Daniel McNeese · in Torque Game Builder · 07/05/2007 (12:47 pm) · 7 replies

I'm experimenting with moving between levels. I'm able to switch between levels well enough, but am having two related problems.

1) How do I make changes to a level permanent, instead of having the level reset whenever I return to it?

2) How do I get objects to move from one level to another? The "removeFromScene()" and "addToScene()" functions seemed like the obvious way, but it's not working.

Here's my test function:

function levelDown()
  {
  $player.removeFromScene();
  SceneWindow2D.endLevel();
  SceneWindow2D.schedule( 0, LoadLevel, expandFileName ("~/data/levels/level2.t2d"));
  $player.addToScene(sceneWindow2D.getSceneGraph());
  $player.setPosition(0, 0);
  }

#1
07/05/2007 (3:39 pm)
The problem might be that you're scheduling the loadLevel command, but running the addToScene right away, so it actually gets called before loadLevel. Note that schedule does not pause execution of the current function.

The solution would be to either schedule your addToScene and setPosition as well, or call them via an onLevelLoaded function.
#2
07/05/2007 (3:54 pm)
Hmm... That makes sense, but scheduling the other commands doesn't fix the problem and I'm not getting any script error reports in the console.

Here's the new code:

function levelDown()
  {
  %temp = sceneWindow2D.getSceneGraph();
  $player.removeFromScene();
  SceneWindow2D.endLevel();
  SceneWindow2D.schedule(0, LoadLevel, expandFileName ("~/data/levels/level2.t2d"));
  $player.schedule(1, "addToScene", %temp);
  $player.schedule(2, "setPosition", 0, 0);
  }

EDIT: This version didn't work either:

function levelDown()
  {
  $player.removeFromScene();
  SceneWindow2D.endLevel();
  SceneWindow2D.schedule(0, LoadLevel, expandFileName ("~/data/levels/level2.t2d"));
  $player.schedule(1, "addToScene", sceneWindow2D.getSceneGraph());
  $player.schedule(2, "setPosition", 0, 0);
  }
#3
07/05/2007 (4:22 pm)
Quick note on schedule: you are scheduling in milliseconds, so you are basically saying "addToScene" in 1 millisecond, and "setPosition" in 2 milliseconds...neither of which are going to work for you.

While a schedule is not too bad of a prototype implementation for something like this, an alternate strategy might be:

--In each of your levels, create a sceneObject (invisible, no collision, etc.) and name it LevelLoadSpawnSpot or something similar.

--write a namespace method called LevelLoadSpawnSpot::onLevelLoaded(...) (it's a callback from the engine, check the reference docs for the arguments). This method should then do the following:

----add the player object to the (new) scene. The onlevelLoaded callback provides the %sceneGraph as a parameter
----move the player object to the location of the spawn spot object (if you wish).

--before ending the level, remove the player from the scene as you do now, then end the level.

--load the new level, and the callback will be executed automatically.

This alleviates all scheduling needs, and also handles any sort of timing issues, since the player isn't going to be added to the (new) scene until the scene is ready for it to happen.
#4
07/05/2007 (4:37 pm)
Quote:Quick note on schedule: you are scheduling in milliseconds, so you are basically saying "addToScene" in 1 millisecond, and "setPosition" in 2 milliseconds...

I knew the argument was milliseconds, but I wanted to make sure the events happened in order and didn't think a 2-millisecond delay would affect things from the player's perspective.


Quote:--write a namespace method called LevelLoadSpawnSpot::onLevelLoaded

.
.
.

--load the new level, and the callback will be executed automatically.

This runs into the other problem, though. Loading a level resets it, which isn't what I want.
#5
07/05/2007 (9:32 pm)
Well, level loading/unloading isn't designed as an auto-serialization mechanism, it's designed to go from a finished level to pristine/starting level (think of a breakout/arkanoid style game).

If you want levels to save previous (changed by the game play) states and restore them on re-visitation, you'll want to roll your own level save/load system (not particularly difficult, but will take a bit of thought).
#6
07/05/2007 (11:03 pm)
I should be able to manage that, once I learn a little more of TGB. Thanks for the help.

One more thing... Where do you go to make feature suggestions?
#7
07/05/2007 (11:06 pm)
There should be a "suggestions" or "feedback" forum for TGB--if not, the bug section is fine as well--or just general discussion :)