メインコンテンツへスキップ

ecs.createStateMachine()

ecs.createStateMachine(world, owner, machineDef)

Description

Create a state machine.

Parameters

PropertyTypeDescription
worldworldworld reference
ownereideid of the entity the state machine belongs to
machineDefStateMachineDefinitionstate machine definition

Returns

Returns id of the created state machine.

StateMachineDefinition

PropertyTypeDescription
initialStatestringName of the starting state of the state machine.
statesRecord<string, State>A map that store state name and their definition.

State

PropertyTypeDescription
triggersRecord<string, Trigger[]>Name of the next states that this state and transition to and the triggers to do so.
onEnter (optional)functiona function called before the state is entered.
onExit (optional)functiona function called before the state exits.

Trigger

There are two type of triggers: EventTrigger and TimeoutTrigger.

EventTrigger

KeyTypeDescription
typeconstant: 'event'a constant to indicate the type of the trigger
eventstringthe event type that trigger this
target (optional)eidthe entity you want this trigger to change state for
beforeTransition (optional)(event) => booleana function that run before the transition, if the result is truthy then the transition will terminate and the state will not change

TimeoutTrigger

KeyTypeDescription
typeconstant: 'timeout'a constant to indicate the type of the trigger
timeoutnumberthe number of ms before the transition