Game Development Community

Need callback for when a scene graph is loaded (when screen loads)

by Jeff Moretti · in Torque Game Builder · 10/20/2015 (9:12 pm) · 10 replies

Dear Torque team,

I am using the function setSceneGraph to load a scene graph into the sceneWindow.

ie. My line of code looks like:
sceneWindow2D.schedule(20,"setSceneGraph", $gameLevel);

However, I am noticing that the scene graph starts running and executing stuff even before the scene graph is visible on the screen. Is there a callback for determining when the screen finally displays to the user?

Thanks in advance,

Jeff

#1
10/21/2015 (1:58 pm)
Hi Jeff!

Without C++ code modification, the best strategy would be to enable your Scene's Render callback

TorqueScript

MyScene.RenderCallback = true

Then, create a new TorqueScript function to handle the callback.
Within this callback function, disable the rendercallback so that it is never called again, else it will be called every frame!

function MyScene::onSceneRender(%this)
{
...
MyScene.RenderCallback = false;
}

Drawback : This will be called after the scene has been rendered; the first rendered frame will be visible on screen when the function is executed.

C++


You will need to find a logical place to put your callback.

My bet would be in SceneWindow::setScene located in /2d/gui/SceneWindow.cc

Just add the following code to the C++ source and recompile.

Con::executef( this, 2, "myScriptCallback" );

Then, in script,

function MyScene::myScriptCallback(%this)
{
...
echo("This works marvelously!");
}

Let us know if you have any further questions!
#2
10/21/2015 (2:31 pm)
Hi Simon,

I greatly appreciate you helping me out here. I have tried the above solution, but alas, it doesnt not seem to work for me. I think the reason is because I am using an old version of torque 2D (v1.7.6). Does the above solution still work for this version of torque? And do I apply the above solution to which class, t2dSceneGraph? Or maybe sceneWindow2D?

Thanks again for all your help,

Jeff
#3
10/21/2015 (3:09 pm)
Oh sorry!

I haven't used TGB 1.7.6. for years, I do not remember. The class structure is a bit different in TGB, I will have to dig around its source when I have time to do so.

Just out of curiosity, why not switch to T2D 3.x?
Lack of Editors, right? :)
#4
10/21/2015 (4:27 pm)
Not exactly. My project is already very close to complete, and I started it way back in 2010, and haven't really want to go through the pain point of converting (at least not yet).

The Editor isn't a big deal to me actually. I can live without it. Im also sort of a coder by nature =)
#5
10/22/2015 (7:38 pm)
Any luck on finding the 'onSceneRender' method equivalent in TGB 1.7.6? No worries if you can't find it, I could live without it. Its more just to cure a 'performance' (ie scene object being choppy) bug than it is a blocking bug.

But anyways, yeah, if you know of an equivalent method in TGB 1.7.6, then please let me know. Thanks!
#6
10/23/2015 (5:45 am)
Okay, try this:

In common/gameScripts/levelManagement.cs, find t2dSceneWindow::addToLevel(). Right before returning, call your function.
#7
10/23/2015 (9:19 am)
Thanks, but no luck. I placed an echo statement right before the function returned, and I never saw the message in the console. I am assuming this didn't work because I do not load level files in my script, I instead create the scenegraph from my code and the set the scenegraph to the scenewindow (ie I never load a 'level', or call functions such as 'loadLevel(%levelFile)').
#8
10/23/2015 (3:34 pm)
Oh, hell, try it right before you set the scenegraph to the window....

EDIT -

And, you can pause the scene. If you create an empty scene, pause it, add stuff to it, set it to the window, then unpause it, just maybe. Of course, you'll have to find another way to handle suspending any schedules you might fire up in the mean time.

EDIT AGAIN -

Or, add a callback at the end of t2dSceneWindow::setSceneGraph(). Something like:
Con::executef( this, 2, "onSetScene" );
#9
10/23/2015 (4:19 pm)
=)

Guess what? All I had to do was like you said, 'try it right before you set the scenegraph to the window'. My apologies for not thinking of that myself.

Thanks for all your help friend :)
#10
10/24/2015 (4:11 am)
Naw, sometimes it just takes a different set of eyes....