165797Re: [flexcoders] Function from string?
- Nov 5, 2013
>> I'm writing a game. Well, actually I guess it's more of a game engine. IHi,
>> 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
>> 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?
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
- << Previous post in topic Next post in topic >>