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
Velocity component, whereas another entity might have a
UI component. Systems are logic that runs on a specific set of component types. You might have a
movement system that runs on all entities with a
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.
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 datatypes for all of these concepts:
for transform in query.iter println!;
Now let's see how this works in practice!
Paste the following function into your
This will be our first system. The only remaining step is to add it to our App!
hello_world.system() function call. This is a "trait extension method" that converts the
hello_world function into the
Now run your App again using
cargo run. You should see
hello world! printed once in your terminal.
Greeting the whole world is great, but what if we want to greet specific people? In ECS, you would generally model people as entities with a set of components that define them. Let's start simple with a
Add this struct to
But what if we want our people to have a name? In a more traditional design, we might just tack on a
name: String field to
Person. But other entities might have names too! For example, dogs should probably also have a name. It often makes sense to break datatypes up in to small pieces to encourage code reuse. So let's make
Name its own component:
We can then add
People to our
using a "startup system". Startup systems are just like normal systems, but they run exactly once, before all other systems, right when our app starts. Let's use
to spawn some entities into our
Now register the startup system like this:
We could run this App now and the
add_people system would run first, followed by
hello_world. But our new people don't have anything to do yet! Let's make a system that properly greets the new citizens of our
The parameters we pass in to a "system function" define what data the system runs on. In this case,
greet_people will run on all entities with the
You can interpret the Query above as: "iterate over every Name component for entities that also have a Person component"
Now we just register the system in our App:
Running our app will result in the following output:
hello world! hello Elaina Proctor! hello Renzo Hume! hello Zayna Nieves!
Quick Note: "hello world!" might show up in a different order than it does above. This is because systems run in parallel by default whenever possible.