Behaviors
BehaviorSystem runs the behaviors attached to actors.
To write a behavior and attach it in the editor, see Behaviors in Game Code.
Setting it up
Section titled “Setting it up”- In the Systems tab, add Behavior System.
- Add
BehaviorSystem,Behaviorsand the project’sgameBehaviorsto the engine:
import { Engine, BehaviorSystem, Behaviors } from 'dacha';
const engine = new Engine({ config, systems: [BehaviorSystem, ...gameSystems], components: [Behaviors, ...gameComponents], assets: [], resources: { [BehaviorSystem.systemName]: [...gameBehaviors], },});The Behaviors component
Section titled “The Behaviors component”The component has one field, list. It holds the behaviors of the actor, in the order they run.
Each entry has three fields:
| Field | What it is |
|---|---|
id |
An identifier for the entry. The editor creates it |
name |
The behaviorName of the class to create |
options |
Values for the behavior’s fields. The system passes them to the constructor |
When instances are created and destroyed
Section titled “When instances are created and destroyed”BehaviorSystem is a scene system, so each scene gets its own copy. It creates the behavior
instances for an actor when:
- the scene becomes active,
- an actor with
Behaviorsis added to the active scene, - an actor in the active scene gets a
Behaviorscomponent.
It calls destroy on the instances of an actor when:
- the actor is removed from the scene,
- the
Behaviorscomponent is removed from the actor, - the scene is destroyed.
Changing behaviors at runtime
Section titled “Changing behaviors at runtime”The system reads Behaviors only when it creates the instances. If you update the component later, the
running instances do not change.
To change the behaviors of an actor, replace the whole component:
actor.setComponent( new Behaviors({ list: [{ id: 'patrol', name: 'Patrol', options: { speed: 2 } }], }),);setComponent removes the old component first, so the system destroys the old instances. Then
it adds the new component, so the system creates instances from the new list.
Where behaviors run in the frame
Section titled “Where behaviors run in the frame”When BehaviorSystem gets its turn in the systems list, it calls update and fixedUpdate on every behavior of every actor.