RE: [flexcoders] Function from string?

2013-11-07 Thread Keith Reinfeld
 I would like to be able to take a 
function name I've read from the script and call the corresponding 
function.  

 

Hi A. P. Damien, 

As I understand it, you have existing functions which you want to call by
passing the name of a function as a string to a generic method. 
By 'script' I take you to mean an external text source. 

public function makeTheCall(s:String):void{ 
   //use try/catch to handle errors
   try {
 this[s]();
   }
   catch(e:ReferenceError) {
 //trace(s,e);
   }
}

Do I have to declare the class 'dynamic' for this trick to work?
I don't think so.



 Or is it always legal to do this from inside the class? 
'Always' is a tricky word. The functions you want to call need to be in the
same Class in order for the 'this' keyword to work. The function's name and
the  function name string must be an exact match for case and spelling. 




Regards,

 

Keith Reinfeld 



RE: [flexcoders] Function from string?

2013-11-06 Thread Keith Reinfeld
Perhaps the following syntax will help:

 

var func:String = yourFunctionName; 

this[func]();  

 

Regards,

 

Keith Reinfeld  

 

From: flexcoders@yahoogroups.com [mailto:flexcoders@yahoogroups.com] On
Behalf Of A. P. Damien
Sent: Tuesday, November 05, 2013 10:48 AM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Function from string?

 

  

I'm writing a game. Well, actually I guess it's more of a game engine. I 
want the game logic to be in a script. The script will take the form of 
a series of rooms, each of which can contain:

. doors to other rooms
. characters the player can interact with
. things the player can pick up and use

Any of these can have conditions on them. The condition can be expressed as:
. Player must have (thing) in his inventory
. Player must have at least X points
. Player must have at least $Y money
. Call function f

That last seems to be a problem. I would like to be able to take a 
function name I've read from the script and call the corresponding 
function. Or, alternatively, the script can contain the name of a class 
which must contain a method named (for example) conditionTest.

But I haven't figured out a way to convert a string to either a Function 
or a Class. Is there a way of doing this? If I were working in 
Javascript, I'd just call eval, but that seems to be forbidden in AS3, 
right?

Can I do this? Or do I have to put all the functions I might want to 
call this way in an array, and look them up that way? Or what?





Re: [flexcoders] Function from string?

2013-11-06 Thread hamann . w
 I'm writing a game. Well, actually I guess it's more of a game engine. I 
 want the game logic to be in a script.  The script will take the form of 
 a series of rooms, each of which can contain:
 
. doors to other rooms
. characters the player can interact with
. things the player can pick up and use
 
 Any of these can have conditions on them. The condition can be expressed as:
. Player must have (thing) in his inventory
. Player must have at least X points
. Player must have at least $Y money
. Call function f
 
 That last seems to be a problem. I would like to be able to take a 
 function name I've read from the script and call the corresponding 
 function. Or, alternatively, the script can contain the name of a class 
 which must contain a method named (for example) conditionTest.
 
 But I haven't figured out a way to convert a string to either a Function 
 or a Class.  Is there a way of doing this?  If I were working in 
 Javascript, I'd just call eval, but that seems to be forbidden in AS3, 
 right?
 
 Can I do this? Or do I have to put all the functions I might want to 
 call this way in an array, and look them up that way?  Or what?
 
Hi,

you can call

this['funcname'] or similar if the class is known.
You can call getDefinition if you need to match the class name
It might make sense to have all the expected functions in an array, just to 
avoid
that a script error calls functions that should not

Regards
Wolfgang





Re: [flexcoders] Function from string?

2013-11-06 Thread A. P. Damien
On 11/6/2013 9:16 AM, Keith Reinfeld wrote:
 Perhaps the following syntax will help:

 var func:String = “yourFunctionName”;

 this[func]();

Thanks. I hadn't fully assimilated the fact that an object can also  be 
treated as an array.  Do I have to declare the class 'dynamic' for this 
trick to work? Or is it always legal to do this from inside the class?




--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Alternative FAQ location: 
https://share.acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-1e62079f6847
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo 
Groups Links

* To visit your group on the web, go to:
http://groups.yahoo.com/group/flexcoders/

* Your email settings:
Individual Email | Traditional

* To change settings online go to:
http://groups.yahoo.com/group/flexcoders/join
(Yahoo! ID required)

* To change settings via email:
flexcoders-dig...@yahoogroups.com 
flexcoders-fullfeatu...@yahoogroups.com

* To unsubscribe from this group, send an email to:
flexcoders-unsubscr...@yahoogroups.com

* Your use of Yahoo Groups is subject to:
http://info.yahoo.com/legal/us/yahoo/utos/terms/



Re: [flexcoders] Function from string?

2013-11-06 Thread Alex Harui
Both Array and Object support [] property access.

Regarding converting a String to a Function:  AS is compiled, not
interpreted, so only strings that have been compiled into functions can be
run.  You can fill an object or array full of functions and choose one at
runtime, but that is not the same as constructing a string at runtime and
trying to run it.  Eval() will do that in JS, but there is no AS
equivalent.

Some options are:

1) There are some AS3 eval() functions in third-party libraries.  But they
are unlikely to be fast.
2) You can send the string to a server, compile it into a SWF and load
that SWF and run it.
3) If the function doesn't require much information from the rest of the
app, you could write the string to JS and call eval on it via
ExternalInterface

-Alex

On 11/6/13 2:02 PM, A. P. Damien apdam...@ca.rr.com wrote:

On 11/6/2013 9:16 AM, Keith Reinfeld wrote:
 Perhaps the following syntax will help:

 var func:String = ³yourFunctionName²;

 this[func]();

Thanks. I hadn't fully assimilated the fact that an object can also  be
treated as an array.  Do I have to declare the class 'dynamic' for this
trick to work? Or is it always legal to do this from inside the class?




--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Alternative FAQ location:
https://share.acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-1e
62079f6847
Search Archives: 
http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo Groups Links