Game Development Community

Help at Level Load.

by Romulo Diniz Filippini · in Torque Game Builder · 03/28/2011 (7:41 am) · 10 replies

Hi,

I'm trying to load a level after the first, the function itself is not the problem, I managed to use it with a schedule.

The real problem is that I have two different scripts: cinematics 1 and cinematics 2, both with a giant OnLevelLoaded function with dozens of other scheduled functions. And I don't know how to make the system knows when load each one of these.

There is a code like this?:

If (level == X)
{
  exec("cinematics1.cs")
}

If (level == Y)
{
  exec("cinematics2.cs")
}

#1
03/28/2011 (7:45 am)
I don't understand your question.

The onLevelLoaded function automatically gets called when you load the level file. It has nothing to do with execing the script.
#2
03/28/2011 (8:08 am)
Oh sorry Chris, I forgot to say that I put the cinematics1.cs and cinematics2.cs on my main.cs, so both kick in at the same time. So the one that comes first in the list is exec-ed and the other one not. So I'd like to exec one in the first level, and another when I change to the second level.
#3
03/28/2011 (9:22 am)
It feels like you don't really understand what the point of exec is.

When you exec a script file you make it so the code inside it is evaluated and turned into usable functions (generally).

You place functions in a script then you exec it and then you can call those functions anywhere in your script. You can exec everything when the game loads if you want to and is probably recommended.

The onLevelLoaded function is a callback function called by the engine when a level file is loaded. the onLevelLoaded function you've implemented should be in a script that you exec before it needs to be called.

You really should Read the docs. Then Read the example code in the tutorials before trying to code it yourself.

Read. Read Code. Code.
#4
03/28/2011 (6:38 pm)
Ok, here is what I'm trying to do:

//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
// initializeProject
// Perform game initialization here.
//---------------------------------------------------------------------------------------------


function initializeProject()
{
// Load up the in game gui.
   exec("~/gui/mainScreen.gui");

   // Exec game scripts.

	if (%level == level1)
	{
	exec("./gameScripts/cinemtics2.cs");
	}
	if (%level == level2)
	{
	exec("./gameScripts/cinematic2.cs");
	}

And I did every tutorial that Garage Games created for new users and some other created by other people and posted at TDN. It would be very handy if there was at least one complete game available with commented functions so that other users could learn from it.
If I was a seasoned programmer it would be simple to scroll thru the reference guides and learn about all torquescript functions, but this is the first time I have contact with programming, so...
The only other source of information about it that I can think of is the book: Torque Guide to Programming, which I do own, but have no time to read right now.
#5
03/29/2011 (5:05 am)
You are comparing %level, which is a local variable and only exists inside of that function. Since you haven't initialized with a value inside the function it will be empty.

What is level1 supposed to be? Do you mean "level1", a string? If that is the case you need to compare with $=

I mean you no offence when I say this but you need to stop coding and go Read the docs, especially the torquescript ones. You need to Read the book you have.
#6
03/29/2011 (6:52 am)
All exec() does is to prepare scripts for use and create the .dso files. If the files contains functions and methods, nothing special will happen. The contents won't be executed - don't let the name of the call fool you :)

This is not a reference (it's only a tri…uh, tutorial):
tdn.garagegames.com/wiki/TorqueScript

That's the first tutorial you need to read if you're completely new to TorqueScript. If you're entirely new to programming, it might help, but I don't think it's the goal to teach people entirely from scratch. Try it and see how far you get anyway :)

#7
03/30/2011 (6:57 pm)
I've read the docs Ronny linked up, and they were pretty helpful at making a lot of concepts much clearer.

After another search here in the forums I found someone that had the same goal:onLevelLoaded() for specific level. That is exactly what I was trying to do.

I followed the advices in this post and managed to get some progress by changing the class name of the scenegraph of each level, and putting the OnLevelLoaded() function in it's namespace:

function level1::onLevelLoaded(%this, %scenegraph)

And now when the specific level is loaded the desired function is executed, but now I have another problem.

Aside the onLevelLoaded() function inside cinematics1.cs and cinematics2.cs, there are plenty other functions that are called by schedules:

function level1::onLevelLoaded(%this, %scenegraph)
{
	schedule(0, 0, playbg);
	schedule(2000, 0, banditmove1);
	schedule(2000, 0, playBanditWalk);
	schedule(5000, 0, banditsays1);
	schedule(5000, 0, npcDialogOpen);
}
//-----------------------------------//
function banditmove1()
{
	Bandit.moveto(-30, 16.016, 10, true, true, true);
	Bandit.playanimation( "Bandit_Walk_no_knifeAnimation" );
}
//-----------------------------------//

Whe I open Level 1 for example, it calls level1::onLevelLoaded() of cinematics1.cs, but also calls every function of cinematics2.cs, and I don't undertand why, since that functions are only called by schedules!
Should I make a package and desactivate those functions?
#8
03/31/2011 (5:19 am)
Try something like:

function level1::onLevelLoaded(%this, %scenegraph)  
{  
    %this.schedule(0, playbg);
    
    schedule(2000, 0, banditmove1);  
    schedule(2000, 0, playBanditWalk);  
    schedule(5000, 0, banditsays1);  
    schedule(5000, 0, npcDialogOpen);  
}

function level1::playbg(%this)
{
}

or... name the functions uniquely for each level.
#9
03/31/2011 (12:25 pm)
Using name spaces in the rest of the functions didn't worked, but renaming them with unique names worked perfectly, I'll be doing this with the other levels too.

It's not translated to english yet, it is in portuguese, but if anyone want to take a look at how it's going: Tachyon Mini-demo.

ASAP I'll translate it and post here so you can see what you all are helping me to make.

Many thanks for the help lend to me till now.
#10
03/31/2011 (12:27 pm)
Post it as a blog so we can check it out once it's in the english. :)