Game Development Community

Question on calling a specific function...

by Jacob · in Torque Game Engine · 10/10/2005 (8:47 am) · 5 replies

Hi all!

Is there a way to call a function where the function name is based on a variable?

Here is an example of what I would like to do:
function myNamespace::myFunction1(%myVar)
{
     (some irrelevant code)
     
     // here I want to call another function that is named with the value of %myVar
     myNamespace::%myVar();
}

Now I realize that the above example will give an error. Is there any way I can accomplish calling a funcion based on a variable without using switch of if statements?

#1
10/10/2005 (9:10 am)
Well, for now I think I will use packages to call the functions I need. This way I can just use the same function name and just activate the package that I need based on %myVar...but any other idead are welcome :)
#2
10/10/2005 (9:14 am)
%myNameSpace = "myNameSpace";
%myVar = "myVar";

%function = %myNameSpace @ "::" @ %myVar @ "();";

eval(%function);
#3
10/10/2005 (9:29 am)
If %myVar was a function pointer it may work with this syntax:

function myNamespace::myFunction1( %myVar)
{
    (some irrelevant code)

    (%myVar)();
}

function myNamespace::myFunction2()
{
   //...
}

// this calls myFunction1 setting %myVar as a function pointer
function someCallingFunction()
{
    myNamespace::myFunction1( myNamespace::myFunction2);
}

Try it and see. I have not tried it.
#4
10/10/2005 (9:31 am)
If you form it into a string you can simply eval() it :)
#5
10/10/2005 (10:04 am)
Thanks guys! I was thinking of eval also but didn't think of how to form it all into a string...Perfect! I am grateful:)