Simple State Machine

A place to post your custom classes and engine extensions to share with the community.

Simple State Machine

Postby StuartTresadern » Sun Dec 08, 2013 12:50 pm

** Updated to generate unnamed transition names using ige.newIdHex() **

Simple state machine ported to work with the isogenic engine.

Add the StateMachine.js file to your game classes folder and reference in your client and or server config.

You can add multiple instances of the state machine so for example the main game could have states, menu, playing etc. and also each entity could have its own state machine for AI or animation transition control.

Entity example:

Instantiate a new instance of the state machine in the entity init method:

Code: Select all
self.fsm = new StateMachine();


Define your states:
In this example the callbacks simply log but you can do whatever logic is required in your game for entry, update and exit of the state. Don’t forget to use self. Inside the callbacks because the reference to this. Will be lost. Each state requires a name and callback functions for the enter, update and exit. You can have as many states as your game requires.

Code: Select all
self.fsm.state('idle',{
            enter: function() { console.log('Entering Idle State'); },
            update: function() { console.log('Updating Idle State');},
            exit: function() { console.log('Exiting Idle State');}
        });

self.fsm.state('moving',{
            enter: function() { console.log('Entering Moving State'); },
            update: function() { console.log('Updating Moving State'); },
            exit: function() { console.log('Exiting Moving State');}
        });


Define the transitions

Transitions are what makes the state change the callback logic with the transition needs to return true or false basically telling the state machine if the transition can take place. transition takes 4 parameters, the transition name, the from state name, the to state name and the callback function. Again you can have as many transition's as your game requires.

Code: Select all
self.fsm.transition('readytogo', 'idle', 'moving', function() {
            return self._box2dBody.GetLinearVelocity().x > 0;
        });

        self.fsm.transition('noenergy', 'moving', 'idle', function() {
            return self._box2dBody.GetLinearVelocity().x == 0;
        });


finally you need to update the state machine, normally in the entity update:

Code: Select all
update: function(ctx){

        this.fsm.update();
        IgeEntityBox2d.prototype.update.call(this, ctx);
    },



You can check the initial, current and previous state :

Code: Select all
        this.fsm.initialState
        this.fsm.currentState
        this.fsm.previousState
Attachments
StateMachine.zip
(1.07 KiB) Downloaded 697 times
Last edited by StuartTresadern on Mon Dec 23, 2013 7:31 pm, edited 1 time in total.
User avatar
StuartTresadern
 
Posts: 59
Joined: Sun Dec 01, 2013 5:23 pm
Location: Germany

Re: Simple State Machine

Postby rob » Wed Dec 18, 2013 7:24 pm

Another great share from Stuart! :)
CEO & Lead Developer
Irrelon Software Limited
http://www.isogenicengine.com
User avatar
rob
Site Admin
 
Posts: 316
Joined: Tue Oct 22, 2013 5:08 pm

Re: Simple State Machine

Postby rob » Mon Dec 23, 2013 5:23 pm

There's a global "unnamedTransitionCounter" in the file. I can't see it declared anywhere, should it be "this.unnamedTransitionCounter"?
CEO & Lead Developer
Irrelon Software Limited
http://www.isogenicengine.com
User avatar
rob
Site Admin
 
Posts: 316
Joined: Tue Oct 22, 2013 5:08 pm

Re: Simple State Machine

Postby StuartTresadern » Mon Dec 23, 2013 6:57 pm

Missed that I'll correct it later and update the source. I think the original defined it as a global var.

I need to double check but I don't think it can be defined as this.unamedTransitionCounter because if you have multiple state machines and define transitions with out names it's possible to end up with multiple transitions with the same name.

Stuart
User avatar
StuartTresadern
 
Posts: 59
Joined: Sun Dec 01, 2013 5:23 pm
Location: Germany

Re: Simple State Machine

Postby rob » Mon Dec 23, 2013 7:04 pm

Ahh in that case you can use the engine's ige.newIdHex() or it's something like that. Take a look at IgeEngine for it. It will generate a unique id every call.
CEO & Lead Developer
Irrelon Software Limited
http://www.isogenicengine.com
User avatar
rob
Site Admin
 
Posts: 316
Joined: Tue Oct 22, 2013 5:08 pm

Re: Simple State Machine

Postby StuartTresadern » Mon Dec 23, 2013 7:06 pm

Great thanks :)
User avatar
StuartTresadern
 
Posts: 59
Joined: Sun Dec 01, 2013 5:23 pm
Location: Germany

Re: Simple State Machine

Postby StuartTresadern » Mon Dec 23, 2013 7:32 pm

The original zip has now been updated to include the recommended change.

Stuatz
User avatar
StuartTresadern
 
Posts: 59
Joined: Sun Dec 01, 2013 5:23 pm
Location: Germany


Return to Custom Code

Who is online

Users browsing this forum: No registered users and 0 guests
cron