Tainted \\ Coders

Explicitly Order Events

Last updated:

By default Bevy will try and run systems that are in the same schedule in parallel as long as they do not both need mutable access to the same data.

When events are written they are added to a double buffered queue and kept for at least two frames to ensure any systems that were processed out of order (because they were run in parallel) will receive the event.

The problem here is you can be reading events from up to two frames ago, which may or may not introduce subtle bugs into your game logic.

To avoid this we should schedule systems that produce events to run before systems that consume them:

use bevy::prelude::*;

struct PlayerLeveledUp(u32);

fn event_producer(mut writer: EventWriter<PlayerLeveledUp>) {

fn event_consumer(mut reader: EventReader<PlayerLeveledUp>) {
  for event in reader.read() {
    println!("Player leveled up to level: {}", event.0);

fn main() {
            // We could also (event_producer, event_consumer).chain()

This works great for events in the same Schedule but how can we order them between schedules?

We can explicitly order the schedules by modifying the MainScheduleOrder resource:

#[derive(ScheduleLabel, Debug, Hash, PartialEq, Eq, Clone)]
struct UpdatePlayers;

#[derive(ScheduleLabel, Debug, Hash, PartialEq, Eq, Clone)]
struct UpdateEnemies;

fn main() {
    let mut app = App::new();
    let update_players_schedule = Schedule::new(UpdatePlayers);
    let update_enemies_schedule = Schedule::new(UpdateEnemies);
    let mut main_schedule_order =

    main_schedule_order.insert_after(Update, UpdatePlayers);
    main_schedule_order.insert_after(UpdatePlayers, UpdateEnemies);

        .add_systems(UpdatePlayers, event_producer)
        .add_systems(UpdateEnemies, event_consumer)

Read more