Game Development Community

Variable stores level currently loaded

by Andrew H · in Torque Game Builder · 08/15/2011 (8:15 pm) · 10 replies

I am going to set up an IF statement so that if the level loaded is the main menu, it loads the main menu GUI. Is there a variable that stores this information? If not, is there another way I can do this?

NOTE: Due to the current nature of my code, I cannot have it load the GUI in the same block of code it loads the level in.

#1
08/17/2011 (6:24 am)
Please? I need help here!
#2
08/17/2011 (4:46 pm)
Hmm, here is what I would do:

Name the main menu level scenegraph either via the level builder or directly editing the scene file, then add a code similar to the following somewhere:

function t2dSceneGraph::onLevelLoaded( %this ){

   //If this is the main level that just loaded
   if(%this.getName() $= <Name>){

       //Change to main level GUI

   }

}
#3
08/17/2011 (4:47 pm)
Oh, okay. I guess I should have thought about that - but I don't really have much programming knowledge. Anyways - thanks!
#4
08/17/2011 (4:52 pm)
Yep no worries, you'll pick it up as you go, and it'll get more fun as you learn how to manipulate it ^^

Torquescript is extremely forgiving and easy to write, so someone who can't really program in a real language can still pick it up easily (like me!).
#5
08/17/2011 (5:51 pm)
Alright, then! Cool. Thanks a lot, Justin!
#6
08/17/2011 (6:43 pm)
Now I'm having a problem. When I try this code, %this.getData() returns blank - instead of the level loaded. Being somebody who is still learning TorqueScript, I have no idea what is wrong.
#7
08/18/2011 (12:58 am)
Most likely, %this have no getData() method. Justin's suggestion uses getName(), which should return name of the object if it has one.
#8
08/18/2011 (8:52 am)
@Rpahut, Oh, right. I meant to say 'getName()'. That was what was in the code...
#9
08/18/2011 (11:59 am)
Well, here goes long explanation then to avoid further misunderstanding.

First of all, you need a level file with a named scenegraph in it. You'll trust level editor to handle most simple tasks, but when there is a problem it's better to see your scripts at first hand. Open your file and make sure name is really there:
%levelContent = new t2dSceneGraph(nameGoesHere) {

To load a level, engine basically executes level file as if it was normal script. New scenegraph object is created with all the fields defined in file, including name.

After that engine will try to call onLevelLoaded() for that new scenegraph. Normally there is no such method defined, but if you add it to your scripts like Justin suggested it will be called, and scenegraph will be passed in as %this.

Now you can script there whatever you want to happen on level load and access scenegraph's name or other fields you can see in the level file. At this point, if scenegraph has a name it should be accessible trough %this.getName(). If it's not, you'll need to track trough this entire process, find a place where it gone wrong and fix it. Since engine code is out of suspicions most part of the time, I would tell it is unnamed scenegraph or onLevelLoaded() being called the-wrong-way.
#10
08/18/2011 (1:20 pm)
@Rpahut, That was the problem! My scenegraph didn't have a name...I'll see if that fixes it.

[after testing]

That did it! I still have a little bug tweaking to do, but that made it work! Thanks - BIG time. And thanks to Justin as well!