StateMachine Utility map command dynamically
Hi,
I've implemented the state machine utility in my development
using the joel hooks' example (https://github.com/joelhooks/robotlegs-utilities-StateMachine).
All work fine.
I'm wondering if we can omit the constant class and dynamically
map events with command.
I thought I could add a "command" attribute in state nodes which
would contain the command class path.
When configuring the state machine, I could iterate the state list
and map events using the getDefinitionByName() method.
Class listed in the XML are not compiled in the application so I
get the classic ReferenceError.
Is there any way to bypass the error without having to reference the class in my app ?
Thanks. lulu.
Comments are currently closed for this discussion. You can start a new one.
2 Posted by neil on 25 Aug, 2011 05:00 PM
What I do is to add an addClass method to the FSMInjector. I then do some reflection on the class ref, and keep the stats. The addClass ensures the class is included. I will add a link to illustrate later as I am mobile at the mo.
3 Posted by vincent on 25 Aug, 2011 05:45 PM
Yes, an example would be great!
4 Posted by neil on 25 Aug, 2011 08:28 PM
hope this explains it:
https://gist.github.com/1171775
if not, feel free to ask more questions.
5 Posted by LulumOriss on 26 Aug, 2011 09:34 AM
Thanks for your reply and your code sample.
My point is to save importing command classes when I initialize the state machine process. All I want to have to do is to create the command class and reference its path in the XML file.
I don't know if it's a good idea after all. What do you think ?
6 Posted by neil on 26 Aug, 2011 10:16 AM
ok,
1) you will have to import your cmds somewhere, otherwise they wont compile.
2) the alternative is to create a command factory
3) You'll also have to map them at some point to the commandMap
( I assume thats what you want to do) surely the best place to map them is when you initialise the FSM
not entirely sure what you are trying to do
7 Posted by vincent on 26 Aug, 2011 10:24 AM
Yes, I may be not clear.
I find the mapping process quite painful and I want to automate it. I initialize the state machine like this :
var l_sti:FSMInjector = new FSMInjector( stmProxy.descriptor );
var l_stm:StateMachine = new StateMachine( eventDispatcher );
DisplayApplicationCommand; // I would like to avoid this line
var l_slt:XMLList = stmProxy.descriptor..state;
var l_ste:XML;
for each( l_ste in l_slt )
{
if( [email blocked]() == 0 || [email blocked]() == 0 )
continue;
commandMap.mapEvent( l_ste.@changed[ 0 ], getDefinitionByName( l_ste.@command[ 0 ] ) as Class );
}
l_sti.inject( l_stm );
What's a command factory?
8 Posted by neil on 26 Aug, 2011 11:17 AM
A factory encapsulates the creation of classes.
basically, you need some where to include the classes, I'm afraid there's no getting away with it, as AS3 doesn't support class loading
9 Posted by vincent on 26 Aug, 2011 11:26 AM
Ok. Thanks a lot.
vincent closed this discussion on 26 Aug, 2011 11:26 AM.