Copy link to clipboard
Copied
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:
fun...
Copy link to clipboard
Copied
Depends on what you mean by that but you can do things like:
var someFunction:Function = function(arg0:uint, arg1:uint):uint {return arg0 + arg1};
trace(someFunction(3, 6));
Copy link to clipboard
Copied
This is almost what I meant, I guess I should have went into more detail (sorry about that), I want to be able to read the contents of an XML file, then dynamically read a function child(<function arg0="int:Num" arg1="String:str" returns="type">function body</function>) then make a Function object out of it which can be called by Actionscript later. I am familiar with the XML classes within Flash, and I know how to parse most primitve data types, but I can't think of how to do make a function body out of a string.
Copy link to clipboard
Copied
There is no way to do that And is not the best to do so anyhow. This would be a pain to debug by any developer.
All things dynamic must have rules and structure to enable it.
Therefore anything you are trying to achieve with xml can be achieved by passing parameters in flash.
you can use the function arguments or ... rest behavior or even 😘 for unknown types to pass in or return.
Copy link to clipboard
Copied
I agree with FeZEC here. I would make an assumption that there is some lgoci in your app that you might not have determine is part of your application. You have have your programming all pre-determined, and remove the "customized" parts (unique content, navigation, etc.) to be in your XML.
Think of your your general rules are first, and then determine what your variety might be to figure out what kind of template you need to build.
Build the plan, and then fly it (rather than flying the plane asa it's built, or being renovated 😉
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Bare in mind, why do you think the eval has been deprecated?
I highly recommend you stear clear of this manner of coding.
AS3 is not set up by nature to publish to ECMA standards.. AS3 has grown, and relies on ECMA for legacy purposes
Copy link to clipboard
Copied
Thanks FeZEC, I would have to agree that what I'm trying to do would be quite unstable at runtime, and while D.eval() does do what I was initially looking to do, it made my program quite buggy.
Copy link to clipboard
Copied
you're trying to "create" a function dynamically or dynamically construct the function name and call it? or, something else?
Copy link to clipboard
Copied
I was trying to dynamically create a function (including the function body) and run it from a vector (ie. functions()). The idea was that a user could input commands at design (similar to a console) but could save them or copy them (depending on platform) and use them later.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more