Skills

Bevy Events Messaging

Bevy ECS skill for messages and observers.

Install

npx skills add thedivergentai/Game-Dev-Agentic-Skills --skill bevy-events-messaging -g -a claude-code -y
Category
Engineering
Updated
19d ago
License
LGPL-3.0
Context cost
Mediumabout 1,710 tokens
Runs scripts
No, instructions only

What it does

Bevy ECS skill for messages and observers. Keywords: Bevy, Message, Event, Observer.

Bevy events and messaging

When to use

Load this when one system should notice that something happened without storing that fact on every entity. Pick the mechanism from the pages below. Do not invent a third channel.

ECS shape

  • Messages (pull). Message is a buffered message for pull-based handling. MessageWriter writes. MessageReader reads and tracks what it already saw. Messages holds what occurred within the last two updates. Register the type with App::add_message. The engine updates the buffer; gameplay systems only write and read.
  • Events (push). Derive Event, add an observer, trigger. Commands::trigger and World::trigger run observers immediately inside the call. On<E> is the system parameter. Event::Trigger defaults to GlobalTrigger for derived events.
  • Observers. An Observer is a component that holds a system. EntityEvent targets one entity: the event carries the entity field, and On::event_target returns it.
  • State transitions. StateTransitionEvent<S> fires when state S changes. That type lives with states. See bevy-state-machines.

Examples

[dependencies]
bevy = "0.20"

A hit is a fact other systems poll. Register the message, write it from the strike system, and read it later in the same schedule. add_message keeps the double-buffered queue.

#[derive(Message)]
struct Hit {
    amount: i32,
}

fn install(app: &mut App) {
    app.add_message::<Hit>()
        .add_systems(Update, (deal, apply).chain());
}

fn deal(mut hits: MessageWriter<Hit>) {
    hits.write(Hit { amount: 12 });
}

fn apply(mut hits: MessageReader<Hit>) {
    for hit in hits.read() {
        info!("hit {}", hit.amount);
    }
}

A shout should run listeners now. Derive Event, observe with On<Shout>, and trigger. The observer runs inside trigger before the system returns.

#[derive(Event)]
struct Shout {
    text: String,
}
Read the whole SKILL.md on GitHub

More in Engineering