Function bevy::ecs::prelude::on_event

source ·
pub fn on_event<T>() -> impl FnMut(EventReader<'_, '_, T>) + Clone
where T: Event,
Expand description

Generates a Condition-satisfying closure that returns true if there are any new events of the given type since it was last called.

Example


app.add_systems(
    my_system.run_if(on_event::<MyEvent>()),
);

#[derive(Event)]
struct MyEvent;

fn my_system(mut counter: ResMut<Counter>) {
    counter.0 += 1;
}

// No new `MyEvent` events have been push so `my_system` won't run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 0);

world.resource_mut::<Events<MyEvent>>().send(MyEvent);

// A `MyEvent` event has been pushed so `my_system` will run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 1);