Loading A New Level
by David Taylor · in Torque Game Builder · 07/18/2006 (4:59 pm) · 17 replies
How do I load a new level from within the game? As in, via code/script. I know how to do it when in the level editor.
About the author
#2
Fastest response ever, by the way, hehe! :)
07/18/2006 (5:08 pm)
Awesome. Do I have to do anything to the previous level? (Such as the push/pop that is done with many of the canvas calls?) Because I want to be able to go back to a previous level at a later stage without having to restart it all over again.Fastest response ever, by the way, hehe! :)
#3
The level I'm trying to load is called StarbaseTestBar.
My scripts are in the standard gameScripts folder, and my levels are in the standard data/levels folder. Can anyone tell me how my pathing is wrong?
Here are a few variations that I've tried:
07/23/2006 (11:55 pm)
Okay, I've tried loading a level, and unfortunately, my only accomplishment has been to crash Torque. :/The level I'm trying to load is called StarbaseTestBar.
My scripts are in the standard gameScripts folder, and my levels are in the standard data/levels folder. Can anyone tell me how my pathing is wrong?
Here are a few variations that I've tried:
function Building::onLevelLoaded(%this, %scenegraph)
{
$Building = %this;
}
function Building::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%srcObj.class == "Player" || %dstObj.class == "Player")
{
sceneWindow2D.loadLevel("./StarbaseTestBar.t2d.dso");
}
}function Building::onLevelLoaded(%this, %scenegraph)
{
$Building = %this;
}
function Building::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%srcObj.class == "Player" || %dstObj.class == "Player")
{
sceneWindow2D.loadLevel("./StarbaseTestBar.t2d.dso");
}
}function Building::onLevelLoaded(%this, %scenegraph)
{
$Building = %this;
}
function Building::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%srcObj.class == "Player" || %dstObj.class == "Player")
{
sceneWindow2D.loadLevel("./data/levels/StarbaseTestBar.t2d");
}
}function Building::onLevelLoaded(%this, %scenegraph)
{
$Building = %this;
}
function Building::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%srcObj.class == "Player" || %dstObj.class == "Player")
{
sceneWindow2D.loadLevel("~/data/levels/StarbaseTestBar.t2d");
}
}function Building::onLevelLoaded(%this, %scenegraph)
{
$Building = %this;
}
function Building::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%srcObj.class == "Player" || %dstObj.class == "Player")
{
sceneWindow2D.loadLevel("~/data/levels/StarbaseTestBar.t2d.dso");
}
}
#4
07/24/2006 (8:47 am)
The first thing that sticks out to me is numerous %blah.class == "Player" which will never give the results you are probably looking for. The == operator is for numeric comparisons only. You'll need to use the $= operator for string comparison. Try %blah $= "Player" for starters and let us know where that gets you.
#5
Those if statements are basically checking to see if the object that is colliding with the building sprite is the player object. If it is, then I want it to load the level. It's the pathing that is my main issue, I think.
But thanks anyway, hehe! ;)
07/24/2006 (5:23 pm)
No, it's not the if statements that I'm having a problem with. It's the level loading itself. Those if statements work fine. I know, because I've added echo commands to make sure.Those if statements are basically checking to see if the object that is colliding with the building sprite is the player object. If it is, then I want it to load the level. It's the pathing that is my main issue, I think.
But thanks anyway, hehe! ;)
#6
I would try this:
If that doesn't work, try tacking on .dso on the end of your path.
Also, if you haven't you'll want to try $= instead of == like Ben said.
07/24/2006 (6:48 pm)
David,I would try this:
if(%srcObj.class == "Player" || %dstObj.class == "Player")
{
sceneWindow2D.endLevel();
sceneWindow2D.loadLevel("~/data/levels/StarbaseTestBar.t2d");
}If that doesn't work, try tacking on .dso on the end of your path.
Also, if you haven't you'll want to try $= instead of == like Ben said.
#7
07/24/2006 (6:55 pm)
Thanks, Kevin. It still crashes though, for some inexplicable reason. I've since set it up with an onEnter trigger which gets triggered, but ending or loading a level makes Torque freeze. I'm completely baffled, now. :(
#8
What you'll want to do is replace your loadLevel call with this:
This will divert the load level call to the start of the next frame.
Also, you are going to want to use '$=' instead of '=='. And, replace 'TGB' in the code above with the name of your project. Using the '~' will most likely not work.
07/24/2006 (7:26 pm)
The reason it's crashing is because the object the callback is being called on is being deleted. The first thing the loadLevel call does is clean up the previous level, so the Building is being destroyed, and basically, the engine gets confused.What you'll want to do is replace your loadLevel call with this:
sceneWindow2D.schedule( 0, loadLevel, "TGB/data/levels/StarbaseTestBar.t2d" );
This will divert the load level call to the start of the next frame.
Also, you are going to want to use '$=' instead of '=='. And, replace 'TGB' in the code above with the name of your project. Using the '~' will most likely not work.
#9
07/24/2006 (7:35 pm)
I too was having trouble loading levels. Turns out I had to enter my project name instead of '~', as you suggested. Also, if "loadLevel" cleans up the previous level does that mean that there's no need to call "endLevel" before?
#10
Changing the == to $= doesn't work, for whatever reason. But == still works fine.
I also changed the onCollision trigger to an onEnter trigger following someone else's advice. So here is the code that got it to work:
07/24/2006 (10:20 pm)
Thanks, Adam. That was the trick.Changing the == to $= doesn't work, for whatever reason. But == still works fine.
I also changed the onCollision trigger to an onEnter trigger following someone else's advice. So here is the code that got it to work:
function Building::onLevelLoaded(%this, %scenegraph)
{
$Building = %this;
}
function Building::onEnter(%this, %object)
{
if(%object.class == "Player")
{
sceneWindow2D.schedule(0, loadLevel, "Stellar/data/levels/StarbaseTestBar.t2d");
}
}
#11
07/24/2006 (11:45 pm)
Glad you got it working. Yep, there is no need to call endLevel. If you don't want to clean up the scenegraph for whatever reason, you can use the function t2dSceneWindow::addToLevel.
#12
Notice torque thinks "player" is equal to "mommy" when using ==
== does not work for strings...
07/24/2006 (11:45 pm)
==>echo("player" == "player");
1
==>echo("player" == "mommy");
1Notice torque thinks "player" is equal to "mommy" when using ==
==>echo("player" $= "player");
1
==>echo("player" $= "mommy");
0== does not work for strings...
#13
07/25/2006 (12:10 am)
I'm not saying you're wrong, Ben. I'm just saying that my code above doesn't load up a new level with the $= operator, despite logic stating otherwise. So for now, I'm just going with what works, rather than what makes sense, hehe! ;)
#14
07/25/2006 (12:22 am)
It probably doesn't load the level when you use $= because the class of your objects is not equal to "player" so when you use the proper comparison, you are getting false. All strings are equal to each other using == so you're always running the load level command, even if the objects colliding are not players.
#15
Is that OnEnter function have to do with bounding boxes or collision polys?
Heres a link to a thread about pathing if anyone is interested.
Link to Thread about Pathing
07/25/2006 (6:19 am)
That's a good point Ben. Is that OnEnter function have to do with bounding boxes or collision polys?
Heres a link to a thread about pathing if anyone is interested.
Link to Thread about Pathing
#16
Kevin - it's for a trigger I added to the level, so I suppose that's closer to a bounding box than anything else.
07/25/2006 (7:21 pm)
Ben - yeah, that was exactly what was happening, it turns out. So now $= works a treat. ;)Kevin - it's for a trigger I added to the level, so I suppose that's closer to a bounding box than anything else.
#17
07/26/2006 (8:52 am)
Ah, a trigger would work much better than the entire building.
Torque 3D Owner Matthew Langley
Torque
Where %level is a path to a level file.