Skills

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
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 splits Person and Name so more than one kind of entity can carry a name.
  • Entities are spawned with Commands in a Startup system. The page spawns a tuple of components: commands.spawn((Person, Name(...))).
  • Systems are functions. Query<&Name, With<Person>> reads names on entities that also have Person. Query<&mut Name, With<Person>> writes. Register with add_systems.
  • Schedule: Startup runs once. Update runs afterward. Systems run in parallel unless you call .chain(). The page chains update_people before greet_people and leaves an unrelated system outside the chain.

Spawn a tuple of components. A parent and child use ChildOf. Change detection uses Changed.

Composition choices

NeedBevy idiom
Has-a parts on one objectSeveral Component structs on one entity
BehaviorA system function over a Query
Spawn at startCommands in a Startup system
Ordered steps.chain() on the systems that depend on each other
Parent and childChildOf 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()));
}
Read the whole SKILL.md on GitHub

More in Engineering