Tutorials

๐ŸŽฎ Game AI Tutorials

Tutorial 1: Patrol Bot with Alert System

Goal: Create an NPC that patrols between waypoints, investigates disturbances, and returns to patrol after investigation.

Difficulty: Beginner | Time: 15 minutes

Tree Structure

Selector "PatrolOrInvestigate"
โ”œโ”€ Sequence "InvestigateDisturbance"
โ”‚  โ”œโ”€ Condition "IsDisturbanceDetected"
โ”‚  โ”œโ”€ Action "MoveToDisturbanceLocation"
โ”‚  โ”œโ”€ Action "LookAround"
โ”‚  โ””โ”€ Action "ReturnToPatrolRoute"
โ”‚
โ””โ”€ Sequence "ContinuePatrol"
   โ”œโ”€ Decorator "RepeatForever"
   โ”‚  โ””โ”€ Sequence "PatrolLoop"
   โ”‚     โ”œโ”€ Action "MoveToWaypoint_A"
   โ”‚     โ”œโ”€ Action "Wait" (duration: 3s)
   โ”‚     โ”œโ”€ Action "MoveToWaypoint_B"
   โ”‚     โ”œโ”€ Action "Wait" (duration: 3s)
   โ”‚     โ””โ”€ Action "MoveToWaypoint_C"

Step-by-Step Instructions

  1. Open Behavior Tree Editor and clear the canvas
  2. Drag a Selector node onto the canvas (this is your root)
  3. Add two children to the Selector:
    • A Sequence named "InvestigateDisturbance"
    • A Sequence named "ContinuePatrol"
  4. Under "InvestigateDisturbance", add:
    • Condition: IsDisturbanceDetected
    • Action: MoveToDisturbanceLocation
    • Action: LookAround
    • Action: ReturnToPatrolRoute
  5. Under "ContinuePatrol", add a Decorator โ†’ RepeatForever, then add a Sequence with patrol actions inside
  6. Export as JSON and test in your game engine
Key Insight

The Selector ensures that investigation takes priority over patrol. When a disturbance is detected, the first branch succeeds and the patrol branch is never evaluated. Once investigation completes, the tree restarts and patrol resumes.

Tutorial 2: Enemy AI with Health-Based Behavior

Goal: Design an enemy that changes tactics based on health level: aggressive when healthy, defensive when injured, and fleeing when critical.

Difficulty: Intermediate | Time: 25 minutes

Tree Structure

Selector "EnemyBehavior"
โ”œโ”€ Sequence "FleeWhenCritical"
โ”‚  โ”œโ”€ Condition "IsHealthBelow" (threshold: 20%)
โ”‚  โ”œโ”€ Action "FindCover"
โ”‚  โ””โ”€ Action "MoveToFleePoint"
โ”‚
โ”œโ”€ Sequence "DefendWhenInjured"
โ”‚  โ”œโ”€ Condition "IsHealthBelow" (threshold: 50%)
โ”‚  โ”œโ”€ Selector "DefensiveActions"
โ”‚  โ”‚  โ”œโ”€ Action "ThrowSmokeGrenade"
โ”‚  โ”‚  โ”œโ”€ Action "CallForBackup"
โ”‚  โ”‚  โ””โ”€ Action "TakeCoverAndHeal"
โ”‚  โ””โ”€ Action "EngageFromDistance"
โ”‚
โ””โ”€ Sequence "AggressiveWhenHealthy"
   โ”œโ”€ Condition "IsHealthAbove" (threshold: 50%)
   โ””โ”€ Selector "AttackPatterns"
      โ”œโ”€ Sequence "MeleeAttack"
      โ”‚  โ”œโ”€ Action "ChargeAtPlayer"
      โ”‚  โ””โ”€ Action "PerformMeleeCombo"
      โ””โ”€ Sequence "RangedAttack"
         โ”œโ”€ Action "TakeAimingPosition"
         โ””โ”€ Action "FireWeapon"

Implementation Tips

๐Ÿค– Robotics Tutorials

Tutorial 3: Mobile Robot Navigation with Obstacle Avoidance

Goal: Build a navigation tree for a warehouse robot that safely navigates to delivery points while avoiding obstacles and humans.

Difficulty: Advanced | Time: 40 minutes

Tree Structure

Selector "SafeNavigation"
โ”œโ”€ Sequence "EmergencyStop"
โ”‚  โ”œโ”€ Condition "IsEmergencyButtonPressed"
โ”‚  โ””โ”€ Action "ImmediateStop"
โ”‚
โ”œโ”€ Sequence "HumanDetected"
โ”‚  โ”œโ”€ Condition "IsHumanInPath"
โ”‚  โ”œโ”€ Action "SlowDown"
โ”‚  โ”œโ”€ Action "AnnouncePresence"
โ”‚  โ””โ”€ Action "NavigateAroundHuman"
โ”‚
โ”œโ”€ Sequence "ObstacleDetected"
โ”‚  โ”œโ”€ Condition "IsObstacleNearby"
โ”‚  โ”œโ”€ Selector "AvoidanceStrategy"
โ”‚  โ”‚  โ”œโ”€ Action "SteerLeft"
โ”‚  โ”‚  โ”œโ”€ Action "SteerRight"
โ”‚  โ”‚  โ””โ”€ Action "BackUpAndReplan"
โ”‚  โ””โ”€ Action "ResumePath"
โ”‚
โ””โ”€ Sequence "NormalNavigation"
   โ”œโ”€ Action "ComputePathToGoal"
   โ”œโ”€ Condition "PathComputed"
   โ”œโ”€ Action "FollowPath"
   โ””โ”€ Condition "ReachedGoal"

Sensor Integration

In a real robot, conditions would read from sensor data:

ROS 2 Integration Example

# Launch file example
ros2 launch bteditor_runner bt_runner.launch.py \
  --tree_file navigation_tree.xml \
  --websocket_port 8765 \
  --tick_rate 10

Tutorial 4: Robotic Arm Pick-and-Place

Goal: Create a manipulation tree for a 6-DOF robotic arm to pick objects from a conveyor belt and place them in bins.

Difficulty: Advanced | Time: 50 minutes

Error Handling Strategy

Manipulation has many failure modes. Wrap each major phase in retry logic:

Decorator "RetryWithBackoff" (max_attempts=3, delay=1s)
โ””โ”€ Sequence "PickObject"
   โ”œโ”€ Action "CaptureImage"
   โ”œโ”€ Action "DetectObjectPose"
   โ”œโ”€ Condition "ObjectDetected"
   โ”œโ”€ Action "PlanGraspTrajectory"
   โ”œโ”€ Action "ExecuteGraspMotion"
   โ”œโ”€ Action "CloseGripper"
   โ””โ”€ Condition "ObjectSecured"

๐Ÿญ Automation Tutorials

Tutorial 5: Automated Testing Sequence

Goal: Design a test runner that validates a complex system through multiple test scenarios with automatic recovery.

Difficulty: Intermediate | Time: 30 minutes

Parallel Test Execution

Use Parallel nodes to run independent tests simultaneously:

Parallel "RunAllTests" (success_threshold=all)
โ”œโ”€ Sequence "TestDatabaseConnection"
โ”‚  โ”œโ”€ Action "PingDatabase"
โ”‚  โ””โ”€ Condition "ResponseReceived"
โ”‚
โ”œโ”€ Sequence "TestAPIEndpoints"
โ”‚  โ”œโ”€ Action "SendHealthCheckRequest"
โ”‚  โ”œโ”€ Condition "StatusCode200"
โ”‚  โ””โ”€ Action "ValidateResponseBody"
โ”‚
โ””โ”€ Sequence "TestMessageQueue"
   โ”œโ”€ Action "PublishTestMessage"
   โ”œโ”€ Action "ConsumeMessage"
   โ””โ”€ Condition "MessageMatches"

๐Ÿ“š Learning Resources

Recommended Reading

Video Tutorials (Coming Soon)

We're producing video versions of these tutorials. Subscribe to our YouTube channel to get notified when they're released.

Community Examples

Check out these community-contributed behavior trees:

Share Your Trees

Built something cool? Share your behavior tree JSON on our GitHub Discussions or tag us on social media. We feature outstanding examples in our monthly newsletter!

โ† Docs index Getting started โ†’