ecs.createStateMachine()
ecs.createStateMachine(world, owner, machineDef)
Description
Create a state machine.
Parameters
Property | Type | Description |
---|
world | world | world reference |
owner | eid | eid of the entity the state machine belongs to |
machineDef | StateMachineDefinition | state machine definition |
Returns
Returns id of the created state machine.
StateMachineDefinition
Property | Type | Description |
---|
initialState | string | Name of the starting state of the state machine. |
states | Record<string, State> | A map that store state name and their definition. |
State
Property | Type | Description |
---|
triggers | Record<string, Trigger[]> | Name of the next states that this state and transition to and the triggers to do so. |
onEnter (optional) | function | a function called before the state is entered. |
onExit (optional) | function | a function called before the state exits. |
Trigger
There are two type of triggers: EventTrigger
and TimeoutTrigger
.
EventTrigger
Key | Type | Description |
---|
type | constant: 'event' | a constant to indicate the type of the trigger |
event | string | the event type that trigger this |
target (optional) | eid | the entity you want this trigger to change state for |
beforeTransition (optional) | (event) => boolean | a function that run before the transition, if the result is truthy then the transition will terminate and the state will not change |
TimeoutTrigger
Key | Type | Description |
---|
type | constant: 'timeout' | a constant to indicate the type of the trigger |
timeout | number | the number of ms before the transition |