State Machine Component


State Machine Component

Everything in a game is based on state. A character walking, jumping, or climbing are all different states. Whether the game is active or over is a different state. This finite state machine is a great component for keeping track of state in your game. It also great for defining the flow of different game states from one state to the next. You can store state in a property for toggling things on and off but using a state machine is a lot more flexible and will allow you to trigger actions whenever the value of the current state is changed. This is more efficient than checking a property every game tick.


There are different types of states. A generic state is the most basic. There are different states that can effect the entity that it is contained on. The Death state will destroy the current entity when it becomes active. A Thinking state will only remain active for a certain length of time. Adding a transition to a state will allow you to dictate which state to transition to next based on some condition. This could be used for complex combos of user input. (i.e. A, A, B, C) If you don't define a transition a state can automatically transition to the default state if the state type is evaluated to false. Generic States do not have conditions so it will alway remain in the current state until explicity changed.

**Note: Currently the state type and transition types do not have property windows defined for you to change in GameBuilder Studio. So just use Generic states for now.

Code Sample

var entity : IEntity = PBE.allocateEntity();

var props : DataComponent = new DataComponent();
props.currentTime = 0;
entity.addComponent(props, "Properties");

var finiteStateMachine : FSMComponent = new FSMComponent();
var walkState : BasicState = new BasicState();

//You could define a transition on this state to check a property
var transition : PropertyTransition = new PropertyTransition(); = new PropertyReference("@Properties.currentTime");
transition.value = "10000"; //10 secs
transition.targetState = "Idle"; //Transition to...
walkState.addTransition( transition );

//You could also trigger an event on the entity whenever the state machine
//changes to the Walk state
walkState.enterEvent = "onStateChanged";

finiteStateMachine.stateMachine.addState("Walk", walkState );

entity.addComponent(finiteStateMachine, "StateMachine");

//You can listen to events on an entity like so
entity.eventDispatcher.addEventListener("onStateChanged", stateChangedFunction);

Follow Us For Updates