# bevy_behave
`bevy_behave` is a behaviour tree plugin for bevy with a sensible API and minimal overheads. No magic is required for the task components, they are are regular bevy components using triggers to report status. When an action node (aka leaf node or task node) in the behaviour tree runs, it will spawn an entity with the components you specified in the tree definition. The tree then waits for this entity to trigger a status report, at which point the entity will be despawned. You can also take actions without spawning an entity by triggering an observed `Event`, which can also be used as a conditional in a control node. This tree definition is from the [chase example](https://github.com/RJ/bevy_behave/blob/main/examples/chase.rs): ```rust let npc_entity = get_enemy_entity(); let player = get_player_entity(); // The tree definition (which is cloneable). // and in theory, able to be loaded from an asset file using reflection (PRs welcome). // When added to the BehaveTree component, this gets transformed internally to hold state etc. // // These trees are `ego_tree::Tree
### Performance
is good.
* There's just one global observer for receiving task status reports from entities or triggers.
* Most of the time, the work is being done in a spawned entity using one of your action components,
and in this state, there is a marker on the tree entity so it doesn't tick or do anything until
a result is ready.
* Avoided mut World systems – the tree ticking should be able to run in parallel with other things.
* So a fairly minimal wrapper around basic bevy systems.
In release mode, i can happily toss 100k enemies in the chase demo and zoom around at max framerate.
It gets slow rendering a zillion gizmo circles before any bevy_behave stuff gets in the way.
**Chase example**
This is the chase example from this repo, running in release mode on an M1 mac with 100k enemies.
Each enemy has a behaviour tree child and an active task component entity. So 1 enemy is 3 entities.
https://github.com/user-attachments/assets/e12bc4dd-d7fb-4eca-8810-90d65300776d
**Video from my space game**
Here I have more complex behaviour trees managing orbits, landing, etc. Lots of PID controllers at work.
No attempts at optimising the logic yet, but I can add 5k ships running behaviours. Each is a dynamic avian physics object exerting forces via a thruster.
https://github.com/user-attachments/assets/ef4f0539-0b4d-4d57-9516-a39783de140f
### Bevy Version Compatibility
| bevy_behave | bevy |
| ----------- | ---- |
| 0.5 | 0.18 |
| 0.4 | 0.17 |
| 0.3 | 0.16 |
| 0.2.2 | 0.15 |
### Chat / Questions?
Say hi in the [bevy_behave discord channel](https://discord.com/channels/691052431525675048/1347180005104422942).
### Further Reading
* Cool interactive blog post using bevy_behave: https://www.hankruiger.com/posts/bevy-behave/
* [Wikipedia on Behavior Trees](https://en.wikipedia.org/wiki/Behavior_tree_(artificial_intelligence,_robotics_and_control))
### License
Same as bevy: MIT or Apache-2.0.