Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only run event systems if they have tangible work to do #7728

Merged
merged 11 commits into from
Sep 24, 2023
7 changes: 5 additions & 2 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,11 @@ impl App {
T: Event,
{
if !self.world.contains_resource::<Events<T>>() {
self.init_resource::<Events<T>>()
.add_system(Events::<T>::update_system.in_base_set(CoreSet::First));
self.init_resource::<Events<T>>().add_system(
bevy_ecs::events::update_system::<T>
.in_base_set(CoreSet::First)
.run_if(bevy_ecs::events::update_condition::<T>),
);
}
self
}
Expand Down
16 changes: 11 additions & 5 deletions crates/bevy_ecs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,6 @@ impl<E: Event> Events<E> {
);
}

/// A system that calls [`Events::update`] once per frame.
pub fn update_system(mut events: ResMut<Self>) {
events.update();
}

#[inline]
fn reset_start_event_count(&mut self) {
self.events_a.start_event_count = self.event_count;
Expand Down Expand Up @@ -665,6 +660,17 @@ impl<E: Event> std::iter::Extend<E> for Events<E> {
}
}

/// A system that calls [`Events::update`] once per frame.
pub fn update_system<T: Send + Sync + 'static>(mut events: ResMut<Events<T>>) {
events.update();
}

/// A run condition that checks if the event's [`update_system`]
james7132 marked this conversation as resolved.
Show resolved Hide resolved
/// needs to run or not.
pub fn update_condition<T: Send + Sync + 'static>(events: Res<Events<T>>) -> bool {
!events.events_a.is_empty() || !events.events_b.is_empty()
}

#[cfg(test)]
mod tests {
use crate::{prelude::World, system::SystemState};
Expand Down