# Examples The examples below are minimal, made-up scenarios to help you get going. For real-world usage of `bonsai-bt` in the wild, see: * [utahrobotics/utah-lunabotics-2026](https://github.com/utahrobotics/utah-lunabotics-2026) — orchestrates core autonomy on a lunar rover. The team placed 3rd out of 50 in the autonomy challenge at [NASA Lunabotics 2026](https://www.nasa.gov/learning-resources/lunabotics-challenge/). * [catornot/bp-ort](https://github.com/catornot/bp-ort) — NPC plugins for Titanfall 2 mod servers (Northstar). [Live demo](https://www.youtube.com/watch?v=uT3aAuB4ej4&t=60s). * [AnotherlandServer/anotherland](https://github.com/AnotherlandServer/anotherland) — NPC behaviors for the Anotherland MMORPG. `cargo build --package examples` ## Game NPC AI console application Demonstrates use of a behavior tree in minimal and easy to follow console application setting where a fictional non-playing game character updates its AI state. Run and inspect this example if you want to get a quick introduction on how behavior tree can be used in an application. `cargo run --bin simple_npc_ai` ## Memoryless composites (`Sequence`/`Select` with `memory = false`) Two short console demos in one binary, showing the difference between memoryless composites and their stateful (memory) siblings: - **memoryless `Sequence`** (`Sequence(...).memory(false)`) — chase while visible. When visibility flips off mid-chase, the running `Chase` aborts on the next tick because the leading `EnemyVisible` check is re-run from scratch. A regular `Sequence` would resume the running child and keep chasing. - **memoryless `Select`** (`Select(...).memory(false)`) — priority preemption. `Attack` is preferred over `Chase`. While the enemy is out of range, attack fails and chase runs. Once the enemy enters range, the next tick preempts the chase and runs attack. `cargo run --bin memoryless_chase` ## Boids flocking Constructing boids flocking behavior by copying the same behavior tree across many agents. Each agent follows the following rules: 1. Fly towards the center of the swarm 2. Avoid other agents and predators (predator being the mouse cursor) 3. Match the velocity of other agents `cargo run --bin boids` *PS!* if for some how you're unable to build and run the example, it is most likely because you're lacking some dependencies. Try installing the following dependencies: `sudo apt-get update && sudo apt-get install libudev-dev pkg-config librust-alsa-sys-dev`

## 3d This is basically a really chaotic 3d animation intended to show how you can create a reactive and responsive animations including behaviors such as shape-shifting, color changes, mouse callback, object rotation and translation and timers. `cargo run --bin 3d`

## Async drone (example for long-running jobs) This is an example to simulate behavior of a drone, where some of the task called in the behavior tree are long-running and must be conducted in background threads to avoid blocking the execution of the tree. The simulated drone will first take off, then if the batteries allow it fly to a goal point and then land. If the batteries are too low, the drone will fly back to the docking station. Finally the drone will land. All of this is done while collision avoidance is running in parallell. `cargo run --bin async_drone`

## Show BT in graphviz Compile the behavior tree into a [graphviz](https://graphviz.org/) compatible [DiGraph](https://docs.rs/petgraph/latest/petgraph/graph/type.DiGraph.html). `cargo run --bin graphviz`

## Behavior Timeout (example for Race behavior and long-running jobs) This simple example shows an example of using the Race behavior to time out a long-running job that takes a random amount of time to complete. `cargo run --bin race_timeout`

## WebSocket visualizer (live tree inspector) A live web-based visualizer for a running behavior tree. The example builds a 30-node tree (one memoryless `Sequence` and one memoryless `Select` included — `memory = false`, both drawn with a dashed circle), enables the visualizer via a single API call `BT::with_telemetry(8910)`, and re-ticks every ~400 ms so leaf statuses (green / yellow / red) and the running-path highlight animate continuously. To see it in example, run the following command: `cargo run --bin visualizer_smoke` Then open in a browser. The tree renders within ~1 s and the status bar reads `connected` / `30 nodes`. `Ctrl-C` and restart — the page reconnects within ≤ 1 s. See the module-level doc comment at [src/visualizer_smoke/main.rs](src/visualizer_smoke/main.rs) for the full DFS tree layout and the per-leaf status-cycle rationale.