Getting Started
Installation
Section titled “Installation”npm install @esengine/behavior-treeBasic Setup
Section titled “Basic Setup”import { Core, Scene } from '@esengine/ecs-framework';import { BehaviorTreePlugin } from '@esengine/behavior-tree';
// Initialize CoreCore.create();
// Install behavior tree pluginconst plugin = new BehaviorTreePlugin();await Core.installPlugin(plugin);
// Create and setup sceneconst scene = new Scene();plugin.setupScene(scene);Core.setScene(scene);Creating Your First Behavior Tree
Section titled “Creating Your First Behavior Tree”Using Builder API
Section titled “Using Builder API”import { BehaviorTreeBuilder, BehaviorTreeStarter } from '@esengine/behavior-tree';
// Build behavior treeconst 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 entityconst entity = scene.createEntity('Guard');BehaviorTreeStarter.start(entity, patrolAI);Node Types
Section titled “Node Types”| Node Type | Description |
|---|---|
| Sequence | Executes children in order until one fails |
| Selector | Tries children until one succeeds |
| Parallel | Executes all children simultaneously |
| Action | Performs a specific action |
| Condition | Checks a condition |
| Decorator | Modifies child behavior |
Blackboard Variables
Section titled “Blackboard Variables”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();Running the Behavior Tree
Section titled “Running the Behavior Tree”// 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 debuggingconst runtime = enemy.getComponent(BehaviorTreeRuntimeComponent);console.log('Current state:', runtime.state);Next Steps
Section titled “Next Steps”- Core Concepts - Understand nodes and execution
- Custom Actions - Create your own nodes
- Editor Guide - Visual tree creation