Behavior Tree Graph Editor - Architecture
Overview
The BT Graph Editor is integrated into BehaviorTreeDebugWindow with dual debug/editor modes. It renders in a standalone SDL3 window with its own ImGui context.
Hotkey: F8
For complete technical details, see BT_Graph_Editor_Architecture.md in the repository.
Core Components
1. BehaviorTreeDebugWindow
Location: Source/AI/BehaviorTreeDebugWindow.h/cpp
Main class with dual functionality:
- Debug Mode: Runtime visualization, entity list, blackboard, execution log
- Editor Mode: Full editing with node creation, validation, save/load
Key members:
bool m_editorMode; // Toggle debug/editor
BTCommandStack m_commandStack; // Undo/redo management
uint32_t m_selectedNodeId; // Current selection
bool m_isDraggingPin; // Connection dragging
BTConfig m_config; // From BT_config.json
2. BTGraphLayoutEngine
Location: Source/AI/BTGraphLayoutEngine.h/cpp
Automatic graph layout using Sugiyama algorithm (5 phases):
- Layering (BFS)
- Initial ordering
- Crossing reduction
- Buchheim-Walker layout
- Force-directed collision resolution
Layout Directions:
TopToBottom- VerticalLeftToRight- Horizontal (default)
3. BTEditorCommand / BTCommandStack
Location: Source/AI/BTEditorCommand.h/cpp
Command pattern for undo/redo (max 100 commands):
AddNodeCommandDeleteNodeCommandMoveNodeCommandConnectNodesCommandDisconnectNodesCommandEditParameterCommand
Keyboard shortcuts: Ctrl+Z (undo), Ctrl+Y (redo)
4. BTConfig
Configuration from Config/BT_config.json:
- Layout settings (spacing, grid size, snap)
- Rendering settings (pin radius, bezier tangent)
- Node colors by type and status
Node System
Node Types
From BTNodeType enum:
Composites:
- Selector, Sequence, Parallel
Decorators:
- Inverter, Repeater, UntilSuccess, UntilFailure, Cooldown
Leaves:
- Action, Condition
Node Statuses
From BTStatus enum:
- Running (yellow/orange)
- Success (green)
- Failure (red)
- Invalid (gray)
Editor Features
Pin-Based Connections
- Input pins (left) and output pins (right)
- Drag from output to input to connect
- Visual feedback (green = valid, red = invalid)
- Bezier curves for visual clarity
Validation System
Real-time validation with BTValidationMessage:
- Error: Critical issues (red)
- Warning: Non-critical (yellow)
- Info: Suggestions (blue)
Rules:
- Exactly one root node
- No cycles
- Decorators: exactly 1 child
- Composites: at least 1 child
Node Properties Editor
Edit selected node:
- Name, action type, condition type
- Repeat count (Repeater)
- Cooldown time (Cooldown)
Save/Load System
JSON V2 Format:
{
"version": 2,
"treeId": 123,
"name": "EnemyAI",
"rootNodeId": 1,
"nodes": [
{
"id": 1,
"type": "Sequence",
"children": [2, 3],
"editorPosition": {"x": 0, "y": 0}
}
]
}
Templates
4 built-in templates:
- Empty - Single root
- Basic AI - Patrol + chase + attack
- Patrol - Waypoint patrol
- Combat - Combat decision tree
Standalone Window Pattern
Same pattern as Animation Editor:
Lifecycle:
CreateSeparateWindow()- SDL_Window, SDL_Renderer, ImGuiContextRenderInSeparateWindow()- Render UI in separate contextDestroySeparateWindow()- Cleanup
Event Handling:
ProcessEvent(SDL_Event*) for window close, keyboard shortcuts, mouse events
Integration
BehaviorTreeAsset
CRUD operations:
uint32_t AddNode(BTNodeType type, const std::string& name);
bool RemoveNode(uint32_t nodeId);
bool ConnectNodes(uint32_t parentId, uint32_t childId);
bool DisconnectNodes(uint32_t parentId, uint32_t childId);
std::vector<BTValidationMessage> ValidateTreeFull() const;
File Locations
Source/AI/BehaviorTreeDebugWindow.h/cpp- Main editorSource/AI/BTGraphLayoutEngine.h/cpp- Layout engineSource/AI/BTEditorCommand.h/cpp- Command patternSource/AI/BehaviorTree.h/cpp- Data structuresConfig/BT_config.json- ConfigurationSource/OlympeEngine.cpp- Main loop (F8 hotkey)
Related Documentation
- BT Graph Editor User Guide - User documentation
- Creating BT Actions - Custom actions
- Behavior Trees Overview - System architecture
See Also
- Complete architecture document in repo:
Docs/Developer/BT_Graph_Editor_Architecture.md - Source code:
Source/AI/