Function bevy_time::common_conditions::on_timer

source ·
pub fn on_timer(duration: Duration) -> impl FnMut(Res<'_, Time>) -> bool + Clone
Expand description

Run condition that is active on a regular time interval, using Time to advance the timer. The timer ticks at the rate of Time::relative_speed.

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(
            Update,
            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.