Skip to content

Commit 3d79dc4

Browse files
nakediblealice-i-cecilemaniwanidjeedai
authored
Unify FixedTime and Time while fixing several problems (#8964)
# Objective Current `FixedTime` and `Time` have several problems. This pull aims to fix many of them at once. - If there is a longer pause between app updates, time will jump forward a lot at once and fixed time will iterate on `FixedUpdate` for a large number of steps. If the pause is merely seconds, then this will just mean jerkiness and possible unexpected behaviour in gameplay. If the pause is hours/days as with OS suspend, the game will appear to freeze until it has caught up with real time. - If calculating a fixed step takes longer than specified fixed step period, the game will enter a death spiral where rendering each frame takes longer and longer due to more and more fixed step updates being run per frame and the game appears to freeze. - There is no way to see current fixed step elapsed time inside fixed steps. In order to track this, the game designer needs to add a custom system inside `FixedUpdate` that calculates elapsed or step count in a resource. - Access to delta time inside fixed step is `FixedStep::period` rather than `Time::delta`. This, coupled with the issue that `Time::elapsed` isn't available at all for fixed steps, makes it that time requiring systems are either implemented to be run in `FixedUpdate` or `Update`, but rarely work in both. - Fixes #8800 - Fixes #8543 - Fixes #7439 - Fixes #5692 ## Solution - Create a generic `Time<T>` clock that has no processing logic but which can be instantiated for multiple usages. This is also exposed for users to add custom clocks. - Create three standard clocks, `Time<Real>`, `Time<Virtual>` and `Time<Fixed>`, all of which contain their individual logic. - Create one "default" clock, which is just `Time` (or `Time<()>`), which will be overwritten from `Time<Virtual>` on each update, and `Time<Fixed>` inside `FixedUpdate` schedule. This way systems that do not care specifically which time they track can work both in `Update` and `FixedUpdate` without changes and the behaviour is intuitive. - Add `max_delta` to virtual time update, which limits how much can be added to virtual time by a single update. This fixes both the behaviour after a long freeze, and also the death spiral by limiting how many fixed timestep iterations there can be per update. Possible future work could be adding `max_accumulator` to add a sort of "leaky bucket" time processing to possibly smooth out jumps in time while keeping frame rate stable. - Many minor tweaks and clarifications to the time functions and their documentation. ## Changelog - `Time::raw_delta()`, `Time::raw_elapsed()` and related methods are moved to `Time<Real>::delta()` and `Time<Real>::elapsed()` and now match `Time` API - `FixedTime` is now `Time<Fixed>` and matches `Time` API. - `Time<Fixed>` default timestep is now 64 Hz, or 15625 microseconds. - `Time` inside `FixedUpdate` now reflects fixed timestep time, making systems portable between `Update ` and `FixedUpdate`. - `Time::pause()`, `Time::set_relative_speed()` and related methods must now be called as `Time<Virtual>::pause()` etc. - There is a new `max_delta` setting in `Time<Virtual>` that limits how much the clock can jump by a single update. The default value is 0.25 seconds. - Removed `on_fixed_timer()` condition as `on_timer()` does the right thing inside `FixedUpdate` now. ## Migration Guide - Change all `Res<Time>` instances that access `raw_delta()`, `raw_elapsed()` and related methods to `Res<Time<Real>>` and `delta()`, `elapsed()`, etc. - Change access to `period` from `Res<FixedTime>` to `Res<Time<Fixed>>` and use `delta()`. - The default timestep has been changed from 60 Hz to 64 Hz. If you wish to restore the old behaviour, use `app.insert_resource(Time::<Fixed>::from_hz(60.0))`. - Change `app.insert_resource(FixedTime::new(duration))` to `app.insert_resource(Time::<Fixed>::from_duration(duration))` - Change `app.insert_resource(FixedTime::new_from_secs(secs))` to `app.insert_resource(Time::<Fixed>::from_seconds(secs))` - Change `system.on_fixed_timer(duration)` to `system.on_timer(duration)`. Timers in systems placed in `FixedUpdate` schedule automatically use the fixed time clock. - Change `ResMut<Time>` calls to `pause()`, `is_paused()`, `set_relative_speed()` and related methods to `ResMut<Time<Virtual>>` calls. The API is the same, with the exception that `relative_speed()` will return the actual last ste relative speed, while `effective_relative_speed()` returns 0.0 if the time is paused and corresponds to the speed that was set when the update for the current frame started. ## Todo - [x] Update pull name and description - [x] Top level documentation on usage - [x] Fix examples - [x] Decide on default `max_delta` value - [x] Decide naming of the three clocks: is `Real`, `Virtual`, `Fixed` good? - [x] Decide if the three clock inner structures should be in prelude - [x] Decide on best way to configure values at startup: is manually inserting a new clock instance okay, or should there be config struct separately? - [x] Fix links in docs - [x] Decide what should be public and what not - [x] Decide how `wrap_period` should be handled when it is changed - [x] ~~Add toggles to disable setting the clock as default?~~ No, separate pull if needed. - [x] Add tests - [x] Reformat, ensure adheres to conventions etc. - [x] Build documentation and see that it looks correct ## Contributors Huge thanks to @alice-i-cecile and @maniwani while building this pull. It was a shared effort! --------- Co-authored-by: Alice Cecile <[email protected]> Co-authored-by: Cameron <[email protected]> Co-authored-by: Jerome Humbert <[email protected]>
1 parent 0294373 commit 3d79dc4

20 files changed

+1586
-855
lines changed

Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,16 @@ description = "Illustrates creating custom system parameters with `SystemParam`"
13981398
category = "ECS (Entity Component System)"
13991399
wasm = false
14001400

1401+
[[example]]
1402+
name = "time"
1403+
path = "examples/ecs/time.rs"
1404+
1405+
[package.metadata.example.time]
1406+
name = "Time handling"
1407+
description = "Explains how Time is handled in ECS"
1408+
category = "ECS (Entity Component System)"
1409+
wasm = false
1410+
14011411
[[example]]
14021412
name = "timers"
14031413
path = "examples/ecs/timers.rs"

crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{Diagnostic, DiagnosticId, Diagnostics, RegisterDiagnostic};
22
use bevy_app::prelude::*;
33
use bevy_core::FrameCount;
44
use bevy_ecs::prelude::*;
5-
use bevy_time::Time;
5+
use bevy_time::{Real, Time};
66

77
/// Adds "frame time" diagnostic to an App, specifically "frame time", "fps" and "frame count"
88
#[derive(Default)]
@@ -30,12 +30,12 @@ impl FrameTimeDiagnosticsPlugin {
3030

3131
pub fn diagnostic_system(
3232
mut diagnostics: Diagnostics,
33-
time: Res<Time>,
33+
time: Res<Time<Real>>,
3434
frame_count: Res<FrameCount>,
3535
) {
3636
diagnostics.add_measurement(Self::FRAME_COUNT, || frame_count.0 as f64);
3737

38-
let delta_seconds = time.raw_delta_seconds_f64();
38+
let delta_seconds = time.delta_seconds_f64();
3939
if delta_seconds == 0.0 {
4040
return;
4141
}

crates/bevy_diagnostic/src/log_diagnostics_plugin.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{Diagnostic, DiagnosticId, DiagnosticsStore};
22
use bevy_app::prelude::*;
33
use bevy_ecs::prelude::*;
44
use bevy_log::{debug, info};
5-
use bevy_time::{Time, Timer, TimerMode};
5+
use bevy_time::{Real, Time, Timer, TimerMode};
66
use bevy_utils::Duration;
77

88
/// An App Plugin that logs diagnostics to the console
@@ -82,10 +82,10 @@ impl LogDiagnosticsPlugin {
8282

8383
fn log_diagnostics_system(
8484
mut state: ResMut<LogDiagnosticsState>,
85-
time: Res<Time>,
85+
time: Res<Time<Real>>,
8686
diagnostics: Res<DiagnosticsStore>,
8787
) {
88-
if state.timer.tick(time.raw_delta()).finished() {
88+
if state.timer.tick(time.delta()).finished() {
8989
if let Some(ref filter) = state.filter {
9090
for diagnostic in filter.iter().flat_map(|id| {
9191
diagnostics
@@ -107,10 +107,10 @@ impl LogDiagnosticsPlugin {
107107

108108
fn log_diagnostics_debug_system(
109109
mut state: ResMut<LogDiagnosticsState>,
110-
time: Res<Time>,
110+
time: Res<Time<Real>>,
111111
diagnostics: Res<DiagnosticsStore>,
112112
) {
113-
if state.timer.tick(time.raw_delta()).finished() {
113+
if state.timer.tick(time.delta()).finished() {
114114
if let Some(ref filter) = state.filter {
115115
for diagnostic in filter.iter().flat_map(|id| {
116116
diagnostics

crates/bevy_gilrs/src/rumble.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bevy_ecs::{
55
};
66
use bevy_input::gamepad::{GamepadRumbleIntensity, GamepadRumbleRequest};
77
use bevy_log::{debug, warn};
8-
use bevy_time::Time;
8+
use bevy_time::{Real, Time};
99
use bevy_utils::{Duration, HashMap};
1010
use gilrs::{
1111
ff::{self, BaseEffect, BaseEffectType, Repeat, Replay},
@@ -120,12 +120,12 @@ fn handle_rumble_request(
120120
Ok(())
121121
}
122122
pub(crate) fn play_gilrs_rumble(
123-
time: Res<Time>,
123+
time: Res<Time<Real>>,
124124
mut gilrs: NonSendMut<Gilrs>,
125125
mut requests: EventReader<GamepadRumbleRequest>,
126126
mut running_rumbles: NonSendMut<RunningRumbleEffects>,
127127
) {
128-
let current_time = time.raw_elapsed();
128+
let current_time = time.elapsed();
129129
// Remove outdated rumble effects.
130130
for rumbles in running_rumbles.rumbles.values_mut() {
131131
// `ff::Effect` uses RAII, dropping = deactivating

crates/bevy_render/src/globals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn extract_frame_count(mut commands: Commands, frame_count: Extract<Res<FrameCou
3939
}
4040

4141
fn extract_time(mut commands: Commands, time: Extract<Res<Time>>) {
42-
commands.insert_resource(time.clone());
42+
commands.insert_resource(**time);
4343
}
4444

4545
/// Contains global values useful when writing shaders.
+2-40
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use crate::{fixed_timestep::FixedTime, Time, Timer, TimerMode};
1+
use crate::{Time, Timer, TimerMode};
22
use bevy_ecs::system::Res;
33
use bevy_utils::Duration;
44

55
/// Run condition that is active on a regular time interval, using [`Time`] to advance
66
/// the timer.
77
///
8-
/// If used for a fixed timestep system, use [`on_fixed_timer`] instead.
9-
///
108
/// ```rust,no_run
119
/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, PluginGroup, Update};
1210
/// # use bevy_ecs::schedule::IntoSystemConfigs;
@@ -40,40 +38,6 @@ pub fn on_timer(duration: Duration) -> impl FnMut(Res<Time>) -> bool + Clone {
4038
}
4139
}
4240

43-
/// Run condition that is active on a regular time interval, using [`FixedTime`] to
44-
/// advance the timer.
45-
///
46-
/// If used for a non-fixed timestep system, use [`on_timer`] instead.
47-
///
48-
/// ```rust,no_run
49-
/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, PluginGroup, FixedUpdate};
50-
/// # use bevy_ecs::schedule::IntoSystemConfigs;
51-
/// # use bevy_utils::Duration;
52-
/// # use bevy_time::common_conditions::on_fixed_timer;
53-
/// fn main() {
54-
/// App::new()
55-
/// .add_plugins(DefaultPlugins)
56-
/// .add_systems(FixedUpdate,
57-
/// tick.run_if(on_fixed_timer(Duration::from_secs(1))),
58-
/// )
59-
/// .run();
60-
/// }
61-
/// fn tick() {
62-
/// // ran once a second
63-
/// }
64-
/// ```
65-
///
66-
/// Note that this run condition may not behave as expected if `duration` is smaller
67-
/// than the fixed timestep period, since the timer may complete multiple times in
68-
/// one fixed update.
69-
pub fn on_fixed_timer(duration: Duration) -> impl FnMut(Res<FixedTime>) -> bool + Clone {
70-
let mut timer = Timer::new(duration, TimerMode::Repeating);
71-
move |time: Res<FixedTime>| {
72-
timer.tick(time.period);
73-
timer.just_finished()
74-
}
75-
}
76-
7741
#[cfg(test)]
7842
mod tests {
7943
use super::*;
@@ -85,9 +49,7 @@ mod tests {
8549
#[test]
8650
fn distributive_run_if_compiles() {
8751
Schedule::default().add_systems(
88-
(test_system, test_system)
89-
.distributive_run_if(on_timer(Duration::new(1, 0)))
90-
.distributive_run_if(on_fixed_timer(Duration::new(1, 0))),
52+
(test_system, test_system).distributive_run_if(on_timer(Duration::new(1, 0))),
9153
);
9254
}
9355
}

0 commit comments

Comments
 (0)