Game Development Community

Function Being Ignored

by Jason Darby · in Torque Game Builder · 08/31/2009 (12:17 pm) · 4 replies

I have a list of function calls E.G. functionA(), followed by startGame() but the first function seems to get ignored, or rather the system doesn't wait for the first to finish before doing the next

main.cs

functionA();
startGame(expandFilename($Game:DefaultScene));

functions.cs

function functionA()
{
// When exit condition is met
return;
}

However if i embed the startGame() function in functionA to get called after the exit condition has been met it works fine

main.cs

functionA();
//startGame(expandFilename($Game:DefaultScene));

functions.cs

function functionA()
{
// When exit condition is met
startGame(expandFilename($Game:DefaultScene));
}

Reading up on C++, C# I would expect code to progress in a linear fashion but it doesn't seem to. Or maybe it's something to do with returning out of functionA? Any help would be much appreciated.

Thanks.

About the author

Computer games book author, have written 6 game creation books, published by Cengage Course Technology. Owner of Arcade website www.madword.com and Castle software.


#1
08/31/2009 (1:02 pm)
I guess the file functions.cs wasn't yet executed in the first example.
#2
08/31/2009 (1:28 pm)
Quote:or rather the system doesn't wait for the first to finish before doing the next

That is expected behavior in TorqueScript. All commands within a give set of curly braces will be executed simultaneously (or at least imperceptibly close to it). So in you main.cs, it would try to execute both of these at the same time:
functionA();
startGame(expandFilename($Game:DefaultScene));
You can put one in a schedule to delay the execution, or place it withing the previous, like you mentioned. Also, if the function has a callback, you can place it in there and it will be fired after the function call is complete.
#3
09/01/2009 (4:18 pm)
Jason, a lot of commands in TorqueScript are asynchronous, which you'd probably agree is a good thing for some functions, such as "walk to there" or "rotate this much". Others, such as addition or setting variable values, are synchronous

Generally, if you do
a();
b();

function a() will complete before function b() does, but the actions invoked by function a() might not. So if you have the functions

function a()
{
    $player.moveTo(leftSide);
}
function b()
{
    $player.moveTo(rightSide);
}

function a() will run through all its lines and exit and only then will function b() get called but the sprite will only walk right, never walking left. Why? Because about 2 milliseconds after you commanded him to walk left, you commanded him to walk right, which wiped out the earlier command

There are a lot of functions that operate asynchronously and it can create quite a headache if you want things to behave synchronously. i eventually wrote my own object, StepManager, to handle things like cut scenes and actions in battle (swipe sword THEN have the damage show up over their head THEN play the death animation)
#4
09/03/2009 (2:22 pm)
Even more - movement described above would not be shown as scripts are executed between frames - so none frame would be refreshed to show left movement.