-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[Merged by Bors] - Add timer-based common run conditions (on_timer
and on_fixed_timer
)
#7866
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::{fixed_timestep::FixedTime, Time, Timer, TimerMode}; | ||
use bevy_ecs::system::Res; | ||
use bevy_utils::Duration; | ||
|
||
/// Run condition that is active on a regular time interval, using [`Time`] to advance | ||
/// the timer. | ||
/// | ||
/// If used for a fixed timestep system, use [`on_fixed_timer`] instead. | ||
/// | ||
/// ```rust,no_run | ||
/// # use bevy_app::{App, IntoSystemAppConfig, NoopPluginGroup as DefaultPlugins, PluginGroup}; | ||
/// # use bevy_ecs::schedule::IntoSystemConfig; | ||
/// # use bevy_utils::Duration; | ||
/// # use bevy_time::common_conditions::on_timer; | ||
/// fn main() { | ||
/// App::new() | ||
/// .add_plugins(DefaultPlugins) | ||
/// .add_system(tick.run_if(on_timer(Duration::from_secs(1)))) | ||
/// .run(); | ||
/// } | ||
/// fn tick() { | ||
/// // ran once a second | ||
/// } | ||
/// ``` | ||
/// | ||
/// Note that this does **not** guarantee that systems will run at exactly the | ||
/// specified interval. If delta time is larger than the specified `duration` then | ||
/// the system will only run once even though the timer may have completed multiple | ||
/// times. This condition should only be used with large time durations (relative to | ||
/// delta time). | ||
/// | ||
/// For more accurate timers, use the [`Timer`] class directly (see | ||
/// [`Timer::times_finished_this_tick`] to address the problem mentioned above), or | ||
/// use fixed timesteps that allow systems to run multiple times per frame. | ||
pub fn on_timer(duration: Duration) -> impl FnMut(Res<Time>) -> bool { | ||
let mut timer = Timer::new(duration, TimerMode::Repeating); | ||
move |time: Res<Time>| { | ||
timer.tick(time.delta()); | ||
timer.just_finished() | ||
} | ||
} | ||
|
||
/// Run condition that is active on a regular time interval, using [`FixedTime`] to | ||
/// advance the timer. | ||
/// | ||
/// If used for a non-fixed timestep system, use [`on_timer`] instead. | ||
/// | ||
/// ```rust,no_run | ||
/// # use bevy_app::{App, CoreSchedule, IntoSystemAppConfig, NoopPluginGroup as DefaultPlugins, PluginGroup}; | ||
/// # use bevy_ecs::schedule::IntoSystemConfig; | ||
/// # use bevy_utils::Duration; | ||
/// # use bevy_time::common_conditions::on_fixed_timer; | ||
/// fn main() { | ||
/// App::new() | ||
/// .add_plugins(DefaultPlugins) | ||
/// .add_system( | ||
/// tick.in_schedule(CoreSchedule::FixedUpdate) | ||
/// .run_if(on_fixed_timer(Duration::from_secs(1))), | ||
/// ) | ||
/// .run(); | ||
/// } | ||
/// fn tick() { | ||
/// // ran once a second | ||
/// } | ||
/// ``` | ||
/// | ||
/// Note that this run condition may not behave as expected if `duration` is smaller | ||
/// than the fixed timestep period, since the timer may complete multiple times in | ||
/// one fixed update. | ||
pub fn on_fixed_timer(duration: Duration) -> impl FnMut(Res<FixedTime>) -> bool { | ||
let mut timer = Timer::new(duration, TimerMode::Repeating); | ||
move |time: Res<FixedTime>| { | ||
timer.tick(time.period); | ||
timer.just_finished() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can do
timer.tick(time.delta()).just_finished()