Game Development Community

Loading a New Level?

by Gellyware · in Torque Game Builder · 02/22/2008 (1:06 am) · 7 replies

When trying to load a new level, TGB keeps crashing.

Here is my syntax:

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


Note 1:
level1.t2d does exist and I've also tried "~game/data/levels/level1.t2d"

Note 2:
If I type sceneWindow2D.loadLevel("game/data/levels/level1.t2d"); in the console, it works properly, however it doesn't from script.


How do you get your level to load?

#1
02/22/2008 (1:34 am)
Are you calling loadLevel from a method of an object? If so, that would account for the crash -- before the new level can be loaded, the old one must be unloaded, which means that the objects in the scene get deleted, including the object that is in the process of executing the method, which causes things to get confused, which leads to the crash.

If this is the problem, instead of trying to call sceneWindow2D.loadLevel from a method, put the loadLevel call in a separate function, and use "schedule" in your method to cause the other function to get called after the method is finished executing.
#2
02/22/2008 (10:21 pm)
Ok that makes sense. I wasn't aware of that...

Thanks Dan, I'll try it out.
#3
02/23/2008 (12:11 am)
Hi again Dan,

Your suggestion doesnt seem to work. I'm still getting the crash even scheduling the event outside of the method.

How is level control typically done?
#4
02/23/2008 (8:06 am)
Can you post your code? Using schedules does work (I've done it myself), so there must be something else going on.
#5
02/24/2008 (2:15 am)
function mga::kill(%this)
{

   
   %this.player.stopMovement();
   %this.player.kill();   
    
   schedule( 4000, endit(), 0);
   schedule( 8000, redirect(), 0);

}



function endit()
{
   sceneWindow2D.endLevel();  
}


function redirect()
{
  
   sceneWindow2D.loadLevel($levelPath @ "level1.t2d"); 
}
#6
02/24/2008 (4:09 am)
The TGB Reference refers to schedules in the form of: < arg1...argN >schedule(time, refobject|0, command,)

So change the kill function to
schedule( 4000, 0, "endit");
   schedule( 8000, 0, "redirect");

Also, you don't have to have to call the endLevel function separately, loadLevel will end the current level before loading the new one.
#7
02/24/2008 (1:19 pm)
Thanks Mike, that fixed it.