Skip to content

Commit 5c6b7d5

Browse files
Ixentussoqb
andauthored
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 <cb.setho@gmail.com>
1 parent 3d99663 commit 5c6b7d5

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

crates/bevy_time/src/common_conditions.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Real, Time, Timer, TimerMode};
1+
use crate::{Real, Time, Timer, TimerMode, Virtual};
22
use bevy_ecs::system::Res;
33
use bevy_utils::Duration;
44

@@ -203,6 +203,38 @@ pub fn repeating_after_real_delay(
203203
}
204204
}
205205

206+
/// Run condition that is active when the [`Time<Virtual>`] clock is paused.
207+
/// Use [`bevy_ecs::schedule::common_conditions::not`] to make it active when
208+
/// it's not paused.
209+
///
210+
/// ```rust,no_run
211+
/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, PluginGroup, Update};
212+
/// # use bevy_ecs::schedule::{common_conditions::not, IntoSystemConfigs};
213+
/// # use bevy_time::common_conditions::paused;
214+
/// fn main() {
215+
/// App::new()
216+
/// .add_plugins(DefaultPlugins)
217+
/// .add_systems(
218+
/// Update,
219+
/// (
220+
/// is_paused.run_if(paused),
221+
/// not_paused.run_if(not(paused)),
222+
/// )
223+
/// )
224+
/// .run();
225+
/// }
226+
/// fn is_paused() {
227+
/// // ran when time is paused
228+
/// }
229+
///
230+
/// fn not_paused() {
231+
/// // ran when time is not paused
232+
/// }
233+
/// ```
234+
pub fn paused(time: Res<Time<Virtual>>) -> bool {
235+
time.is_paused()
236+
}
237+
206238
#[cfg(test)]
207239
mod tests {
208240
use super::*;
@@ -215,7 +247,9 @@ mod tests {
215247
#[test]
216248
fn distributive_run_if_compiles() {
217249
Schedule::default().add_systems(
218-
(test_system, test_system).distributive_run_if(on_timer(Duration::new(1, 0))),
250+
(test_system, test_system)
251+
.distributive_run_if(on_timer(Duration::new(1, 0)))
252+
.distributive_run_if(paused),
219253
);
220254
}
221255
}

0 commit comments

Comments
 (0)