Bevy ECS
What is Bevy ECS?
Bevy ECS is an Entity Component System custom-built for the Bevy game engine. It aims to be simple to use, ergonomic, fast, massively parallel, opinionated, and featureful. It was created specifically for Bevy's needs, but it can easily be used as a standalone crate in other projects.
ECS
All app logic in Bevy uses the Entity Component System paradigm, which is often shortened to ECS. ECS is a software pattern that involves breaking your program up into Entities, Components, and Systems. Entities are unique "things" that are assigned groups of Components, which are then processed using Systems.
For example, one entity might have a Position
and Velocity
component, whereas another entity might have a Position
and UI
component. You might have a movement system that runs on all entities with a Position and Velocity component.
The ECS pattern encourages clean, decoupled designs by forcing you to break up your app data and logic into its core components. It also helps make your code faster by optimizing memory access patterns and making parallelism easier.
Concepts
Bevy ECS is Bevy's implementation of the ECS pattern. Unlike other Rust ECS implementations, which often require complex lifetimes, traits, builder patterns, or macros, Bevy ECS uses normal Rust data types for all of these concepts:
Components
Components are normal Rust structs. They are data stored in a World
and specific instances of Components correlate to Entities.
use *;
Worlds
Entities, Components, and Resources are stored in a World
. Worlds, much like std::collections
's HashSet
and Vec
, expose operations to insert, read, write, and remove the data they store.
use World;
let world = default;
Entities
Entities are unique identifiers that correlate to zero or more Components.
use *;
let mut world = new;
let entity = world
.spawn
.id;
let entity_ref = world.entity;
let position = entity_ref..unwrap;
let velocity = entity_ref..unwrap;
Systems
Systems are normal Rust functions. Thanks to the Rust type system, Bevy ECS can use function parameter types to determine what data needs to be sent to the system. It also uses this "data access" information to determine what Systems can run in parallel with each other.
use *;
Resources
Apps often require unique resources, such as asset collections, renderers, audio servers, time, etc. Bevy ECS makes this pattern a first class citizen. Resource
is a special kind of component that does not belong to any entity. Instead, it is identified uniquely by its type:
use *;
let mut world = new;
world.insert_resource;
let time = world..unwrap;
// You can also access resources from Systems
The resources.rs
example illustrates how to read and write a Counter resource from Systems.
Schedules
Schedules run a set of Systems according to some execution strategy. Systems can be added to any number of System Sets, which are used to control their scheduling metadata.
The built in "parallel executor" considers dependencies between systems and (by default) run as many of them in parallel as possible. This maximizes performance, while keeping the system execution safe. To control the system ordering, define explicit dependencies between systems and their sets.
Using Bevy ECS
Bevy ECS should feel very natural for those familiar with Rust syntax:
use *;
// This system moves each entity with a Position and Velocity component
Features
Query Filters
use *;
;
;
// Gets the Position component of all Entities with Player component and without the Alive
// component.
Change Detection
Bevy ECS tracks all changes to Components and Resources.
Queries can filter for changed Components:
use *;
// Gets the Position component of all Entities whose Velocity has changed since the last run of the System
// Gets the Position component of all Entities that had a Velocity component added since the last run of the System
Resources also expose change state:
use *;
;
// Prints "time changed!" if the Time resource has changed since the last run of the System
The change_detection.rs
example shows how to query only for updated entities and react on changes in resources.
Component Storage
Bevy ECS supports multiple component storage types.
Components can be stored in:
- Tables: Fast and cache friendly iteration, but slower adding and removing of components. This is the default storage type.
- Sparse Sets: Fast adding and removing of components, but slower iteration.
Component storage types are configurable, and they default to table storage if the storage is not manually defined.
use *;
;
;
Component Bundles
Define sets of Components that should be added together.
use *;
;
let mut world = new;
// Spawn a new entity and insert the default PlayerBundle
world.spawn;
// Bundles play well with Rust's struct update syntax
world.spawn;
Events
Events offer a communication channel between one or more systems. Events can be sent using the system parameter EventWriter
and received with EventReader
.
use *;
A minimal set up using events can be seen in events.rs
.
Observers
Observers are systems that listen for a "trigger" of a specific Event
:
use *;
let mut world = new;
world.observe;
world.flush;
world.trigger;
These differ from EventReader
and EventWriter
in that they are "reactive". Rather than happening at a specific point in a schedule, they happen immediately whenever a trigger happens. Triggers can trigger other triggers, and they all will be evaluated at the same time!
Events can also be triggered to target specific entities:
use *;
;
let mut world = new;
let entity = world.spawn_empty.id;
world.observe;
world.flush;
world.trigger_targets;