Copy link to clipboard
Copied
Hi All,
Is it possible to convert string as executable code? like the below one.
For ex:
var fun1 = 'function myFun(arg){
trace(arg);
}'
execute(fun1);
Thanks,
Guru
Copy link to clipboard
Copied
Hi.
Try this:
var fun1:Function = function myFun(arg)
{
trace(arg);
};
this["fun1"]("working");
Copy link to clipboard
Copied
Hi JoãoCésar,
Thanks for your quick reply. Its very nice. But I'm trying something different. Let me explain more.
I have a xml called "action_scripts.xml" and in that xml I have some functions inside CDATA. I get that xml data in flash and try to execute that scripts in run time. I will get that functions as string from the xml and I want it to convert as executable functions inside as3.
action_scripts.xml
<?xml version="1.0" encoding="utf-8"?>
<action_scripts>
<action_script><![CDATA[function fun1(){var a=5; var b=5; var c = a+b; trace('result = '+c)}]]></action_script>
<action_script><![CDATA[function fun2(){var a=5; var b=5; var c = a+b; trace('result = '+c)}]]></action_script>
</action_scripts>
test.fla
var urlLoader:URLLoader;
var urlRequest:URLRequest;
var loader:Loader;
var myXMLData:XML;
urlRequest = new URLRequest("action_scripts.xml");
urlLoader = new URLLoader(urlRequest);
urlLoader.addEventListener(Event.COMPLETE, urlLoaderEventComplete);
function urlLoaderEventComplete(e:Event){
myXMLData = new XML(e.target.data);
executeFun(myXMLData.action_script[0]);
}
function executeFun(fun){
trace(fun) // result as string -> "function fun1(){var a=5; var b=5; var c = a+b; trace('result = '+c)}"
var myFun = fun as Function;
this["myFun"](); // Here the code should work as real function
}
When I published, I'm getting the below error
TypeError: Error #1006: value is not a function.
How can I convert a value/string as real executable function? Is it possible? Please advice me .
Thanks,
Guru
Copy link to clipboard
Copied
Oh I see.
I think this thread is what you need: How to interpret code from string in as3? .
Copy link to clipboard
Copied
Hi JoãoCésar,
Thanks you again for the reply.
I already checked that code. But, it seems that one is related to AS2 and not AS3. The eval() is no more in AS3 I think. Any other options?
Thanks,
Guru
Copy link to clipboard
Copied
There are some links in that thread to third party libraries that are alternatives to the eval function:
http://code.google.com/p/as3scriptinglib/
If those don't work, you will have to consider using JavaScript's eval function through the ExternalInterface class:
Copy link to clipboard
Copied
Thank you JoãoCésar. Let me try all.