Game Development Community

Function overloading

by praju · in Torque Game Builder · 08/04/2010 (7:06 am) · 5 replies

Hi guys,

How do you do function overloading in TGB????

Eg( in c++) : void scale(int value);
void scale(Vector 2D);

I need the functions in TGB. One takes a normal value like integer and the other takes a vector.
I hope it makes sense.

Thanks in advance!!

#1
08/04/2010 (7:37 am)
You can use 2 things here:

1) Attach a class to a function
function className::func() {
}

2) Packages
package packageName1 {
 function func() {
 }
};

package packageName2 {
 function func() {
 }
};
Now activate the package that you want to use.
activatePackage(packageName1);
func();
#2
08/04/2010 (8:10 am)
Here's an okay way to do it:
function scaleValue( %value )
{
  %numWords = getWordCount( %value );
  if( %numWords == 1 )
  {
    return %value * 5;
  }
  else if( %numWords == 2 )
  {
    return t2dVectorScale( %value, 5 );
  }
  else
  {
    error( "scaleValue error: invalid input [" @ %value @ "]" );
  }
}

If you don't mind me asking, why do you want to overload a function like this? Why not just write 2 separate functions?
#3
08/04/2010 (8:43 am)
Thanks guys!!!

@Williams: I usually code in C++!! So i just wanted to put my C++ code into TGB script, thats all!! Sure, i can write two separate functions, but just wanted to know whether TGB script supported it!!! Anyways thanks for your input..!:)
#4
08/04/2010 (8:33 pm)
You just have to remember that all parameters in torquescript functions are actually strings. That's why function overloading can't work.
#5
08/05/2010 (4:08 am)
Yes Alain, i too was thinking the same..!!!
Thanks for pointing it out.. :)