Game Development Community

Beginner question on TorqueScript execution flow

by Koh Hwee Miin · in Torque 3D Beginner · 08/18/2010 (6:54 am) · 6 replies

When programming in TorqueScript, there are script functions and callbacks, the callbacks are functions invoked by engine on certain event, such as onMouseMove().

When are these engine callbacks invoked? Are engine queuing the events until the end of current script execution (until all lines of script are executed)? Or engine will interrupt current script execution to process the events callbacks? For example:

main.cs:
function onMouseMove()
{
echo("mouseMove");
}
echo("hello");
echo("world");

In this case, the output will always be:
> hello
> world
> mouseMove

or is it possible to produce output as:
>hello
>mouseMove
>world

#1
08/18/2010 (5:08 pm)
A callback happens immediately. Keep in mind the majority of callbacks are associated with a class or object. Example:

In GuiBitMapButtonCtrl:
onDefaultClick_callback();

In script, that shows up as:
function GuiBitMapButtonCtrl::onDefaultClick(%this)
{
   // Do code here
}

There are a few exceptions, like onServerQueryStatus(). So, with that in mind, you are misunderstanding the function execution. In your example:

function onMouseMove()
{
   echo("mouseMove");
}
echo("hello");
echo("world");

When that file gets executed, you will always see the "hello" and "world" strings printed to the console. That's because they are called, not declared. onMouseMove is declared, so it does not actually get called until an event occurs somewhere else. If you wanted to, you can achieve a different order by the following:

function onMouseMove()
{
   echo("mouseMove");
}
echo("hello");
onMouseMove();
echo("world");

#2
08/19/2010 (2:11 am)
Thanks for your reply, Michael. But I guess I failed to express my question correctly, please allow me to rephrase my question:

Does the event callback (e.g. mouse move, mouse click and etc) behaves like interrupt? When an event occurs, does Torque engine halt current script execution and jump to the callback?

Following is a simplified script (skipped all the necessary engine/game/network initialization) with a simple GUI with a single button on it.
main.cs:
new GuiWindowCtrl(TestGui){
....
new GuiButtonCtrl() {
....
command = "onTestButtonClick();"
}
}

function onTestButtonClick() {
echo("interrupt!");
}

Canvas.setContent(TestGui);
for(%i = 0;%i < 10000;%i++) echo(%i);

In this case, the output will always be continuous of:
0
1
2
...
9999

Or is it possible for user to interrupt the output by clicking on the button:
0
1
2
....
500
501
interrupt!
.....
9999
#3
08/21/2010 (6:14 am)
I don't believe for/while loops can be interrupted.
Try something like the following
new GuiWindowCtrl(TestGui){
....
new GuiButtonCtrl() {
....
command = "onTestButtonClick();"
}
}

function onTestButtonClick() {
echo("interrupt!");
}

Canvas.setContent(TestGui);

function printNumber(%num)
{
   if (%num > 9999)
      return;
   echo(%num);
   schedule(10, 0, printNumber, %num++);
}
printNumber(0);
You should be able to interrupt that.
I'm not sure if you could interrupt it if you just called printNumber directly without a schedule, but you can test that.
#4
08/23/2010 (2:16 am)
Do you mean that Torque engine will not handle any event until it is done with current script execution?
#5
08/23/2010 (2:56 am)
Yes, Torque doesn't actually interrupt, it just calls callbacks when nothing is processing.
#6
08/23/2010 (4:39 am)
Got it, thanks Jonathan.