bevy_ecs::schedule::common_conditions

Function on_event

Source
pub fn on_event<T: Event>() -> impl FnMut(EventReader<'_, '_, T>) -> bool + Clone
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);