From b66ecc7dc4c44b52d5d1ecceeb04d9f51c5b0dca Mon Sep 17 00:00:00 2001 From: Ixentus Date: Fri, 12 Jan 2024 23:18:57 +0100 Subject: [PATCH] Add paused run condition (#11313) # Objective - It is common to run a system only when the clock is paused or not paused, but this run condition doesn't exist. ## Solution - Add the "paused" run condition. --- ## Changelog - Systems can now be scheduled to run only if the clock is paused or not using `.run_if(paused())` or `.run_if(not(paused()))`. --------- Co-authored-by: radiish --- crates/bevy_time/src/common_conditions.rs | 38 +++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/bevy_time/src/common_conditions.rs b/crates/bevy_time/src/common_conditions.rs index a862e9b2ce715b..a175e4632acb48 100644 --- a/crates/bevy_time/src/common_conditions.rs +++ b/crates/bevy_time/src/common_conditions.rs @@ -1,4 +1,4 @@ -use crate::{Real, Time, Timer, TimerMode}; +use crate::{Real, Time, Timer, TimerMode, Virtual}; use bevy_ecs::system::Res; use bevy_utils::Duration; @@ -203,6 +203,38 @@ pub fn repeating_after_real_delay( } } +/// Run condition that is active when the [`Time`] clock is paused. +/// Use [`bevy_ecs::schedule::common_conditions::not`] to make it active when +/// it's not paused. +/// +/// ```rust,no_run +/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, PluginGroup, Update}; +/// # use bevy_ecs::schedule::{common_conditions::not, IntoSystemConfigs}; +/// # use bevy_time::common_conditions::paused; +/// fn main() { +/// App::new() +/// .add_plugins(DefaultPlugins) +/// .add_systems( +/// Update, +/// ( +/// is_paused.run_if(paused), +/// not_paused.run_if(not(paused)), +/// ) +/// ) +/// .run(); +/// } +/// fn is_paused() { +/// // ran when time is paused +/// } +/// +/// fn not_paused() { +/// // ran when time is not paused +/// } +/// ``` +pub fn paused(time: Res>) -> bool { + time.is_paused() +} + #[cfg(test)] mod tests { use super::*; @@ -215,7 +247,9 @@ mod tests { #[test] fn distributive_run_if_compiles() { Schedule::default().add_systems( - (test_system, test_system).distributive_run_if(on_timer(Duration::new(1, 0))), + (test_system, test_system) + .distributive_run_if(on_timer(Duration::new(1, 0))) + .distributive_run_if(paused), ); } }