Dynamic Function Calling
I am attempting to dynamically populate a function at runtime and then have the ability to call this function. Anybody know how this could be done?
I am attempting to dynamically populate a function at runtime and then have the ability to call this function. Anybody know how this could be done?
What you need is eval(). Unfortunately eval() is not available in AS3, although it is defined in ECMA script standard. (It was in AS1/2.) ![]()
Fortunately, there are some very clever people who produced their own implementations
I will use D.eval by RIA 1 here. http://www.riaone.com/products/deval/
Let's say you have this XML:
<function name="exampleFunction" arg0="num:int" arg1="str:String" returns="String">return num + str</function>
First you have to convert it to a string in AS3 Function format:
function exampleFunction(num:int, str:String):String{return num + str};
Then evaluate it as Function, and execute it.
...
import r1.deval.D;
...
var xml:XML = <function name="exampleFunction" arg0="num:int" arg1="str:String" returns="String">return num + str</function>;
var functionString:String = "function " + xml.@name + "(" + xml.@arg0 + ", " + xml.@arg1 + "):" + xml.@returns + "{" + xml + "};";
var dynamicFunction:Object = D.parseFunctions(functionString);
trace(D.eval("exampleFunction(3, ' spacemen');", null, dynamicFunction));
Traces
3 spacemen
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.