Skip to content

Commit

Permalink
Add paused run condition (#11313)
Browse files Browse the repository at this point in the history
# 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 <[email protected]>
  • Loading branch information
Ixentus and soqb authored Jan 12, 2024
1 parent 3d99663 commit 5c6b7d5
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions crates/bevy_time/src/common_conditions.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -203,6 +203,38 @@ pub fn repeating_after_real_delay(
}
}

/// Run condition that is active when the [`Time<Virtual>`] 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<Time<Virtual>>) -> bool {
time.is_paused()
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -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),
);
}
}

0 comments on commit 5c6b7d5

Please sign in to comment.