
Bevy Ecs Composition
Compose Bevy 0.20 entities from small components and systems.
Install
npx skills add thedivergentai/Game-Dev-Agentic-Skills --skill bevy-ecs-composition -g -a claude-code -y- Repository
- thedivergentai/Game-Dev-Agentic-Skills917
- Category
- Engineering
- Updated
- 19d ago
- License
- LGPL-3.0
- Context cost
- Mediumabout 1,911 tokens
- Runs scripts
- No, instructions only
What it does
Compose Bevy 0.20 entities from small components and systems. Use when spawning tuples with Commands, filtering Query with With, ordering writers with chain, or attaching ChildOf. Keywords: Bevy, ECS, Component, Query, Commands, chain, ChildOf.
Bevy ECS composition
When to use
Load this when gameplay data should be split across small components and systems, instead of one type that owns everything. The Quick Start ECS page is the source for the names below.
ECS shape
- Components are Rust structs that derive
Component. The ECS page splitsPersonandNameso more than one kind of entity can carry a name. - Entities are spawned with
Commandsin aStartupsystem. The page spawns a tuple of components:commands.spawn((Person, Name(...))). - Systems are functions.
Query<&Name, With<Person>>reads names on entities that also havePerson.Query<&mut Name, With<Person>>writes. Register withadd_systems. - Schedule:
Startupruns once.Updateruns afterward. Systems run in parallel unless you call.chain(). The page chainsupdate_peoplebeforegreet_peopleand leaves an unrelated system outside the chain.
Spawn a tuple of components. A parent and child use ChildOf. Change detection uses Changed.
Composition choices
| Need | Bevy idiom |
|---|---|
| Has-a parts on one object | Several Component structs on one entity |
| Behavior | A system function over a Query |
| Spawn at start | Commands in a Startup system |
| Ordered steps | .chain() on the systems that depend on each other |
| Parent and child | ChildOf on the child entity |
Scope
- Tuple spawn,
ChildOf,Changed, and.chain()are in the examples below. - Editor widget trees are out of scope. Composition here is entities, components, and systems.
[package]
name = "ecs-composition"
version = "0.1.0"
edition = "2021"
[dependencies]
bevy = "0.20"
People carry a name. Greeting runs after the rename, in one chain.
#[derive(Component)]
struct Person;
#[derive(Component)]
struct Name(String);
fn spawn_people(mut commands: Commands) {
commands.spawn((Person, Name("Ada".to_string())));
commands.spawn(Name("crate".to_string()));
}
