Game Development Community

First Time question about changing levels

by Steven Herrnstadt · in Torque Game Builder · 03/05/2007 (1:33 pm) · 13 replies

I've read through a lot of forum posts on changing levels, but most of them say something along the lines of "Oh, it's easy, just use the loadLevel() function."

Where at? What's the syntax? I understand that loadLevel is called on a scenegraph with a level parameter, but for someone who just completed the platformer tutorial, there's no mention of scenegraphs beyond naming them.

Do I need to create another .cs file, or should it go in game.cs? I don't think it's supposed to go in player.cs. Do I need the full path or file extension?

#1
03/05/2007 (2:39 pm)
@Steven, "for someone who just completed the platformer tutorial" ... AND, should have also completed the 4 or 5 tutorials that came with TGB (Fish Demo, Fish Game, Whack a Mole, Scroller Demo, etc) as well as read up some more on TDN ... and looked in the TGB Reference (knowing that LoadLevel is a scenegraph function ... ) ... you shock me...

Either way ...

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

The first one, assuming your scene window is referenced with 'sceneWindow2D' (the default, unless you've renamed it) will end the current level -- which is absolutely necessary ... you can not have two levels loaded at once!

The second one, note the full path using the TGB.EXE directory as the 'root' ... loads a new level ...
#2
03/05/2007 (3:03 pm)
Actually, calling loadLevel unloads the current one automatically - so you don't even need endLevel ;)

Also worth noting is that you shouldn't call this from within an object that is going to be unloaded along with the level. That's kinda like building bricks on bricks that you're taking out...or something ;)

A nice way around that is to simply schedule loadLevel with a time of 0, so it'll get called the next frame.
#3
03/05/2007 (3:10 pm)
@Tom, I've run into quite a few issues where calling loadLevel() just locks up the engine, unless I call endLevel first -- is the endLevel being called during loadLevel something that was added to 1.1.2 or 1.1.3?
#4
03/05/2007 (3:21 pm)
Hmmm...I'm not sure actually. The most recent time I used it was in 1.1.5 ;)
So, I'm not sure... It can't hurt to do both though.

- Just checked, and (at least in the 1.1.5 build I'm using for my GG stuff) endLevel is called at the top of loadLevel.

- Checked a bunch more versions and they all seem to have the same endLevel command at the beginning of loadLevel.

Maybe your problems were somehow related to loading from an object in the level being unloaded?

Weird nevertheless.
#5
03/05/2007 (4:50 pm)
@Tom, Quite possibly -- my first few attempts at loading various levels were unbelievably miserable attempts ... very poor code implementations ... I found a niche, and I'm sticking with it ... until I found something 'better' ..

Thanks for the input, either way ... :)
#6
03/07/2007 (12:18 pm)
I have this in player.cs:

if(%dstObj.class $= "door")
{
if ($player.upPressed)
{
schedule(0, "loadInterior", 0);
}
}

and this in game.cs:
function loadInterior()
{
sceneWindow2D.loadLevel("TestGame/data/levels/interior.t2d");
}

If I understand correctly, I don't want to use the object method version because the door will be un-created during loadLevel. So I'm trying to use the global version.
#7
03/07/2007 (12:45 pm)
Looks fine at first glance, does it work?
#8
03/07/2007 (1:50 pm)
It doesn't appear to do anything. I run to the door and press up (and I know the function is called because I have an echo statement right before the schedule statement) and nothing happens. I mean I can continue running around and whatnot but the scene doesn't change.
#9
03/07/2007 (2:22 pm)
Does the console give any error reports? It will let you kow if you gave an incorrect path.
#10
03/07/2007 (5:15 pm)
You should also place an echo in the loadInterior, to check if it's ever being called ...

if(%dstObj.class $= "door")
{
  if ($player.upPressed)
  {
     schedule(50, "loadInterior();", 0);
  }
}

//and this in game.cs:
function loadInterior()
{
  echo("schedule called: loadInterior");
  sceneWindow2D.loadLevel("TestGame/data/levels/interior.t2d");
}

you could also create a globally scoped non-scene object, such as a ScriptObject to manage your level load using timers and the 'setTimerOn' ... I don't believe a non-scene object would be affected by the timer issues mentioned earlier --
#11
03/09/2007 (10:35 am)
The loadInterior function isn't being called - there's an echo statement in there that never prints. I'll try the non-scene object approach and see if that doesn't do the trick.
#12
03/09/2007 (2:03 pm)
Your schedule syntax is wrong. Looking in the TGB reference schedule parameters are this

schedule(%time, %refObject, %command, %args)

you also don't need the parens or the semicolon in the function to call.

try this
schedule(50, 0, "loadInterior");

That should do the trick.
#13
03/15/2007 (1:56 pm)
Thanks, that did it.