Game Development Community

dev|Pro Game Development Curriculum

Fixing the return keyword in TorqueScript

by Andreas Kirsch · 09/26/2007 (12:55 pm) · 6 comments

Download Code File

The return keyword is currently broken in TorqueScript (as of TGB version 1.1.3/1.5).
The following things can happen:
function returnSomething() { return -1; }
function callIt( returnSomething(); }

echo( callIt() );

> -1

or even simpler:
function assignAString() { %a = "hello world!"; }

echo( assignAString() );

> hello world!

You can test this in your favorite Torque console if you don't believe me.
It happens because there is only one opcode for returning from functions and the return value of a function simply is the value of the last evaluated string expression.

There are two ways to fix this:
a) Change the compiler code in such a way that current string buffer is set to "" at when a function ends or return is called without an expression.
b) Add another opcode to distinguish between void returns and returns that return a value

I've chosen the second approach, mainly because my code for the first was a bit hackish and it would require add string constants, etc. It would be easily feasible too though.

Instead I've added another opcode OP_RETURN_VOID and changed the code to use it instead of the generic OP_RETURN where it is appropriate.
I've also bumped the DSOVersion to 38 to invalidate old DSOs (- the normal DSO version is 36 in my version of TGB but if you have applied my other patch, it'll already be at 37).

If anyone wants a cleaner version or has cleaned mine up some more, I'll happily update this resource.

To apply either do it by hand or use the (*NIX) patch tool (the patch was created with SVN Tortoise's "Create Patch").
A command-line that works for me quite well is:
patch -p0 -i patchFileName.diff

#1
09/26/2007 (6:52 pm)
FYI - Perl does the same thing
#2
09/27/2007 (7:54 am)
So does MaxScript.
Its not entirely uncommon in languages, but I don't know if it was intentional here or not and I wasn't aware that it did happen until now. So, thank you.
#3
09/27/2007 (1:24 pm)
interesting, thanks.
#4
12/04/2007 (7:38 pm)
Does this cause any negative effects?

Additionally, could you have a look at your colorization resource?
#5
12/11/2007 (5:24 pm)
This must be a TGE only issue, as I've been working with NULL returns in T2D without name variables being sent out.
#6
01/01/2008 (3:37 pm)
I'm actually using TGB/T2D (and I mention that in the text, too), so it was a TGB issue foremost.

Regarding negative effects:
Maybe you know GG's FSM class (t2dFSM.cs) whose state function returns the state to switch to or "" if it shouldn't switch.
Now you may well guess that you'd normally expect a function that doesn't return anything explicitly to, well, not return anything. But TorqueScript's default behavior is different which can cause interesting bugs, if e.g. your state code happens to call a function that returns a valid state name and your state function doesn't explicitly return "" if you don't want the state changed.

Now that you know of it, you can, of course, change the way you code to avoid the problem altogether and it's certainly cleaner, too, but it's a behavior that I wouldn't expect from the language, so I've fixed it to be more newbie-friendly in this respect.

Greetings,
Andreas