Skip to content

Getting Started

Terminal window
npm install @esengine/behavior-tree
import { Core, Scene } from '@esengine/ecs-framework';
import { BehaviorTreePlugin } from '@esengine/behavior-tree';
// Initialize Core
Core.create();
// Install behavior tree plugin
const plugin = new BehaviorTreePlugin();
await Core.installPlugin(plugin);
// Create and setup scene
const scene = new Scene();
plugin.setupScene(scene);
Core.setScene(scene);
import { BehaviorTreeBuilder, BehaviorTreeStarter } from '@esengine/behavior-tree';
// Build behavior tree
const patrolAI = BehaviorTreeBuilder.create('PatrolAI')
.defineBlackboardVariable('targetPosition', null)
.sequence('PatrolSequence')
.log('Start patrol', 'Patrol')
.wait(2000)
.log('Move to next point', 'Patrol')
.end()
.build();
// Attach to entity
const entity = scene.createEntity('Guard');
BehaviorTreeStarter.start(entity, patrolAI);
Node TypeDescription
SequenceExecutes children in order until one fails
SelectorTries children until one succeeds
ParallelExecutes all children simultaneously
ActionPerforms a specific action
ConditionChecks a condition
DecoratorModifies child behavior

The blackboard is a shared data store for behavior tree nodes:

const tree = BehaviorTreeBuilder.create('EnemyAI')
// Define variables
.defineBlackboardVariable('health', 100)
.defineBlackboardVariable('target', null)
.defineBlackboardVariable('isAlert', false)
.selector('Main')
// Use blackboard in conditions
.sequence('AttackBranch')
.blackboardCompare('health', 30, 'greater')
.blackboardCondition('target', (t) => t !== null)
.log('Attacking', 'Combat')
.end()
.log('Retreating', 'Combat')
.end()
.build();
// The behavior tree runs automatically via ECS system
// Just create the entity and start the tree
const enemy = scene.createEntity('Enemy');
BehaviorTreeStarter.start(enemy, patrolAI);
// Access runtime for debugging
const runtime = enemy.getComponent(BehaviorTreeRuntimeComponent);
console.log('Current state:', runtime.state);