Can script functions return a value?
by Steven Peterson · in Torque Game Engine · 03/10/2006 (2:03 pm) · 4 replies
I have a function that I want to return a value, but i'm getting a syntax error. Can script functions have a return value? the documentation doesn't cover this.
Also if I just use a parameter instead, eg. myFuncion( %rValue ) is the parameter passed by value or refference?
thanks
Also if I just use a parameter instead, eg. myFuncion( %rValue ) is the parameter passed by value or refference?
thanks
#2
My understanding is that perameters are passed by text, since there are no types in Torquescript. So if you pass an object id or name, calling methods on that perameter will call it on the actual object. Suppose you have a function
EDIT: Tom beat me to it while I was posting :P
03/10/2006 (2:36 pm)
I'm a newbie myself, but script functions can return values. You don't have to specify the return type in the function header. Just type return(value);where you want to return something.
My understanding is that perameters are passed by text, since there are no types in Torquescript. So if you pass an object id or name, calling methods on that perameter will call it on the actual object. Suppose you have a function
function increaseSpeed(%objectSpeed)
{
%objectSpeed += 2;
} and you call increaseSpeed(%myPlayer.getSpeed());or
increaseSpeed(%myPlayer.speed);You are passing the text returned by that function, or stored in that variable, not the variable. So the function WILL NOT increase your players speed, it will increase the value you passed by 2. If you want to adjust variables, you typically need to write a function like
increaseSpeed(%objectID)
{
%objectID.speed += 2;
} So if you call increaseSpeed(%myPlayer.getID());That code will act on your speed variable. Hope that helps.
EDIT: Tom beat me to it while I was posting :P
#3
This is all good except return is not a function. The following is correct:
03/11/2006 (3:31 am)
Quote:I'm a newbie myself, but script functions can return values. You don't have to specify the return type in the function header. Just type
return(value);
This is all good except return is not a function. The following is correct:
%position = %player.getTransform(); return %position; // <-------------------
#4
03/11/2006 (7:17 am)
Stefan is correct (and Noah had an excellent write-up!): the syntax error is because you have the ( and ) around the value you want to return. That's a common c++ syntax style, but it confuses TorqueScript unfortunately...you need to not use anything around the return value.
Associate Tom Spilman
Sickhead Games
function GetName() { return "Tom"; } echo( "Hello Torque! -- " @ GetName() );Basic types are passed by value and object instances are passed as a numerical id pointer which is in essence by reference.