Function bevy::time::common_conditions::on_real_timer

pub fn on_real_timer(
    duration: Duration,
) -> impl FnMut(Res<'_, Time<Real>>) + Clone
Expand description

Run condition that is active on a regular time interval, using Time<Real> to advance the timer. The timer ticks are not scaled.

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

Examples found in repository?
examples/time/virtual_time.rs (line 28)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .add_systems(
            Update,
            (
                move_virtual_time_sprites,
                move_real_time_sprites,
                toggle_pause.run_if(input_just_pressed(KeyCode::Space)),
                change_time_speed::<1>.run_if(input_just_pressed(KeyCode::ArrowUp)),
                change_time_speed::<-1>.run_if(input_just_pressed(KeyCode::ArrowDown)),
                (update_virtual_time_info_text, update_real_time_info_text)
                    // update the texts on a timer to make them more readable
                    // `on_timer` run condition uses `Virtual` time meaning it's scaled
                    // and would result in the UI updating at different intervals based
                    // on `Time<Virtual>::relative_speed` and `Time<Virtual>::is_paused()`
                    .run_if(on_real_timer(Duration::from_millis(250))),
            ),
        )
        .run();
}