Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Dynamic Function Calling

Guest
Feb 25, 2011 Feb 25, 2011

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?

TOPICS
ActionScript
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 26, 2011 Feb 26, 2011

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
...
Translate
Community Expert ,
Feb 25, 2011 Feb 25, 2011

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));
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 25, 2011 Feb 25, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 25, 2011 Feb 25, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 25, 2011 Feb 25, 2011

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 😉

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 26, 2011 Feb 26, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 26, 2011 Feb 26, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 12, 2011 Mar 12, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 13, 2011 Mar 13, 2011

you're trying to "create" a function dynamically or dynamically construct the function name and call it?  or, something else?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 18, 2011 Mar 18, 2011
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines