Game Development Community

How to load a level?

by Carnell Grant · in Torque Game Builder · 12/04/2007 (4:33 pm) · 17 replies

This may be a very simple question but I can't find out for the left of me any where on how to do it.

Can you please tell me how to load a level in script? I know that there is a loadlevel function but don't know how to use it, please help.

About the author

Recent Threads


#1
12/04/2007 (5:38 pm)
Well it depends on what makes the level load (getting to a area, killing everything, etc.) try looking on google.
#2
12/04/2007 (6:49 pm)
You really just use it with the filename as the argument. The problem most people seem to have is with the pathing, which can be a tad confusing.
#3
12/05/2007 (9:39 am)
Something like:

ThisSceneGraph::onMouseDown(%this...
{
Schedule(1, LoadLevel, "the level file name");
}

...sorry, but I don't have TGB in front of me right now. Besides the path, the biggest pain is scheduling it. It needs to be shceduled because it's fired from within an event from an object in the scene. If you load the level within a method, the object is destoryed and the function causes errors when it returns (the scene it belongs to is gone!).
#4
12/06/2007 (2:28 pm)
Now what if that still isn't working here is some code to look at:

//startGame("~/data/levels/level2.t2d");(this is what I was using to try to load level)
%this.getScenegraph().schedule(1,"loadLevel","~/data/levels/ Level2.t2d");(I tryed this as well and it still crashed)
//sceneWindow2D.loadLevel("~/data/levels/Level2.t2d"); (even tried this)

Any suggestions would be helpful. thanks.
#5
12/07/2007 (10:44 am)
How about:

SceneWindow2D.Schedule(1, "LoadLevel", "~/data/levels/Level2.t2d");

You want the scene window to load the level rather than the scene graph. And, you need it to be scheduled so that the function you're in has a chance to finish and clean up it's memory so it can unload cleanly.

Does this help? I really need to remember to look at my own script. Sorry my previous post wasn't syntactically correct.
#6
01/13/2008 (11:45 am)
I too keep getting a INVALID FILE error. I am using this same method.


SceneWindow2D.Schedule(1, "LoadLevel", "~/data/levels/Level2.t2d");
#7
03/16/2008 (6:35 am)
I have the same problem.
this is my code:
function NewBut::onMouseDown(%this, %modifier, %worldPos, %mouseClicks)
{
SceneWindow2D.schedule(10,"LoadLevel","~/data/levels/Gamer.t2d");
}
but just, a blank black page load.
#8
03/16/2008 (7:05 am)
I had a problem with schedule the other day, it gave me a parse error in torsion saying I had the wrong number of arguments.

Try using this and see if it works:

function NewBut::onMouseDown(%this, %modifier, %worldPos, %mouseClicks)
{
SceneWindow2D.schedule( 10, %this, LoadLevel, "~/data/levels/Gamer.t2d" );
}

You can also substitute 0 for %this to send a null object.

According to www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2962 the format for the schedule function is: schedule(%time, %object, %command, %args...). Based on that documentation scedule was trying to use your "LoadLevel" as the object not the command.

It worked for me, hope that helps.
#9
03/16/2008 (8:26 am)
It didn't work at all. i changed the code to this:
function NewBut::onMouseDown(%this, %modifier, %worldPos, %mouseClicks)
{
SceneWindow2D.schedule( 10, %this, LoadLevel, "~\data\levels\Gamer.t2d" );
}
and it didn't work to. Shall i do something in LevelEditor? or just should write this code?
i have a menu and i want to load the game if the new command clicked. but it didn't work. plz help me.
#10
03/17/2008 (11:03 am)
Ok, i see you guys have a lots of problems with Loading levels, and i had the same issue in my early days, so i know what pain in the a55 that can be :)

the problem is:

- if you tie function to the object that is IN the scene, bare in mind that he will be destroyed when loading new levels. If You load a level over a destroyed object, the TGB will crash.

- Remember that even scheduled functions can not be tied to destroyed objects, but you can use $globalScriptObject as "object" for schedule();

- I think it is supposed to load levels via GUI, since GUI objects can last trough levels not depending of scenegraph.

You can use more solutions. Personally i wasnt using that technology, but here is my old code (that worked):

///////////////////////////////////
//   Written by Milan Ranchich   //
//    EIPIX game development     //
//         www.eipix.com         //
// Contact: starcraft@sbb.co.yu  //
///////////////////////////////////



// i check level loading on EVERY onMouseUp() event since "mouse" exist in every level, always.
function sceneWindow2D::onMouseUp(%this, %modifier, %worldPosition, %clicks)
{
	// other code (if any)

	levelCheck(); // you must leave this function at the bottom of this event-function!
}




// this i call every time onMouseUp function
function levelCheck()
{
	if($newLevel !$= 0){
		sceneWindow2D.endLevel(); // we destroy (clean up) current level

		sceneWindow2D.loadLevel( $newLevel );
		$newLevel = 0;
	}
}




// this is the code for button that you press, when you want to load new level
function buttonForLevelChange::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
	$newLevel = expandFileName("~/data/levels/level2.t2d");
}


i use "expandFileName("...");" function to define better the file name.



//========================================
// version 2
//========================================


Try this for schedule technology (i did not tested it):

$globalLevelLoader = new ScriptObject();

schedule(30, $globalLevelLoader, "levelCheck");

Explanation:

30 = number in miliseconds. Dont use too small numbers like "1". Think about it, at least one frame must pass when you call this schedule from some object, and the frame when calling-object wil be destroyed by loadLevel function. If you render 100 FPS, then one frame lasts 10Ms. So you can freely use 50, or even 500, nobody will notice the delay.

$globalLevelLoader = this is everLasting object that must exist in all levels in order for loadLevel to work. So try to use this "virtual" scriptObject for this one.

"leveCheck" = this must be a string (you must have quotes!) also i dont think you can call integrated torque function for this one coz you usually must have some object before their functions. syntax for the one that you wanted to use is:

sceneWindow2D.loadLevel(...);

So instead, i use my (previously mentioned) "levelCheck" function that calls sceneWindow2D.loadLevel(...); from within itself.

Forum tip:
Try using [ code ] ... [ /code ] when you write code in your posts (without spaces). it will look like mine above.


Happy torquing ^_^
#11
04/11/2008 (12:21 pm)
Here's what I do...On my Level 1, I have a door that lets my character complete the level. The script name for my door is "enterLevel2" and the script class for my character is "dude". Then, I set up a collision function...

function enterLevel2::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%dstObj.class $= "dude")
{
loadLevel1EndGui();
}
}

Now, "Level1EndGui" is the name of a GUI that I set up. It lists the score, items that were acquired on level 1, etc. The most important thing on my Level1EndGui is the "Begin Level 2" button. The "Begin Level 2" button is a GuiButtonCtrl. Tied to my "Begin Level 2" button is the following command...

loadEnterLevel2();

Now I set up functions for "loadLevel1EndGui" and "loadEnterLevel2".

function loadLevel1EndGui()
{
canvas.setContent(Level1EndGui);
schedule(300, 0);
}

function loadEnterLevel2()
{
sceneWindow2D.loadLevel("game/data/levels/level2.t2d");
sceneWindow2D.mount($dude);
canvas.setContent(sceneWindow2D);
}

Here's basically what happens. The "dude" collides with the door. Boom, the Level1EndGui shows up and shows me the score and acquired items. Then, when I click on the "Begin Level 2" button, voila, Level 2 loads up with my character ready to continue his journey. Hope this helps.
#12
04/16/2008 (8:39 pm)
So milan do you think it's possible to write a behavior that loads a particular level on a particular event related to the object it is applied to? the fields would be:

Load level on click (true false)
Load level on collision (true false)
Load level on remove (true false)
Level to load (levels names)

I think this would be an extremely useful tool (if it can be done) Can work as a guy button, Level exit, End level boss etc...
#13
04/17/2008 (9:08 am)
Yes, that should be easy to write.

The only thing to watch out for, is, if the object with the behavior loading the level exists in the same scenewindow/scenegraph as you are loading the new level for - it will essentially be trying to delete itself, which could cause a crash. I haven't tried this - but it causes crashes when you try to delete the %this/%srcObj in a onCollision callback - and this seems similar.

But there are lots of ways around this, like the object/behavior doesn't directly call loadlevel, instead it starts a schedule in 1 ms that loads it.
#14
04/21/2008 (5:14 pm)
Cool, do you think you can write it?
If anyone could it would be MUCH appreciated.
#15
03/08/2009 (10:57 pm)
hi
i am new with TGB & i am trying to load new level & got same problem
each time(INVALID FILE). But then i enter the full path of level & it
works.

instead of using

"~/data/levels/level2.t2d";

use this

"C:/Documents and Settings/vijay.kandrikar/Desktop/Dummy/MyGame/game/data/levels/level2.t2d";

#16
03/09/2009 (5:37 am)
Well, does that file exist? If you're being told "invalid file", then that would be where to start looking.
#17
03/10/2009 (4:02 pm)
Don't use the full path, that will backfire if you ever hand the game to someone else. Use "MyGame/game/data/levels/leve2.t2d". If you use the name of the folder your game sits in, it'll always work, even if you move the folders around. And you are expanding the file name right? Using "ExpandFileName(...)"?