Skip to content

Commit 7989cb2

Browse files
committed
Add global time scaling (#5752)
# Objective - Make `Time` API more consistent. - Support time accel/decel/pause. ## Solution This is just the `Time` half of #3002. I was told that part isn't controversial. - Give the "delta time" and "total elapsed time" methods `f32`, `f64`, and `Duration` variants with consistent naming. - Implement accelerating / decelerating the passage of time. - Implement stopping time. --- ## Changelog - Changed `time_since_startup` to `elapsed` because `time.time_*` is just silly. - Added `relative_speed` and `set_relative_speed` methods. - Added `is_paused`, `pause`, `unpause` , and methods. (I'd prefer `resume`, but `unpause` matches `Timer` API.) - Added `raw_*` variants of the "delta time" and "total elapsed time" methods. - Added `first_update` method because there's a non-zero duration between startup and the first update. ## Migration Guide - `time.time_since_startup()` -> `time.elapsed()` - `time.seconds_since_startup()` -> `time.elapsed_seconds_f64()` - `time.seconds_since_startup_wrapped_f32()` -> `time.elapsed_seconds_wrapped()` If you aren't sure which to use, most systems should continue to use "scaled" time (e.g. `time.delta_seconds()`). The realtime "unscaled" time measurements (e.g. `time.raw_delta_seconds()`) are mostly for debugging and profiling.
1 parent cb5e2d8 commit 7989cb2

24 files changed

+609
-166
lines changed

crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ impl FrameTimeDiagnosticsPlugin {
3535
) {
3636
diagnostics.add_measurement(Self::FRAME_COUNT, || frame_count.0 as f64);
3737

38-
if time.delta_seconds_f64() == 0.0 {
38+
let delta_seconds = time.raw_delta_seconds_f64();
39+
if delta_seconds == 0.0 {
3940
return;
4041
}
4142

42-
diagnostics.add_measurement(Self::FRAME_TIME, || time.delta_seconds_f64() * 1000.);
43+
diagnostics.add_measurement(Self::FRAME_TIME, || delta_seconds * 1000.0);
4344

44-
diagnostics.add_measurement(Self::FPS, || 1.0 / time.delta_seconds_f64());
45+
diagnostics.add_measurement(Self::FPS, || 1.0 / delta_seconds);
4546
}
4647
}

crates/bevy_diagnostic/src/log_diagnostics_plugin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl LogDiagnosticsPlugin {
8585
time: Res<Time>,
8686
diagnostics: Res<Diagnostics>,
8787
) {
88-
if state.timer.tick(time.delta()).finished() {
88+
if state.timer.tick(time.raw_delta()).finished() {
8989
if let Some(ref filter) = state.filter {
9090
for diagnostic in filter.iter().flat_map(|id| {
9191
diagnostics
@@ -110,7 +110,7 @@ impl LogDiagnosticsPlugin {
110110
time: Res<Time>,
111111
diagnostics: Res<Diagnostics>,
112112
) {
113-
if state.timer.tick(time.delta()).finished() {
113+
if state.timer.tick(time.raw_delta()).finished() {
114114
if let Some(ref filter) = state.filter {
115115
for diagnostic in filter.iter().flat_map(|id| {
116116
diagnostics

crates/bevy_render/src/globals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn prepare_globals_buffer(
5757
frame_count: Res<FrameCount>,
5858
) {
5959
let buffer = globals_buffer.buffer.get_mut();
60-
buffer.time = time.seconds_since_startup_wrapped_f32();
60+
buffer.time = time.elapsed_seconds_wrapped();
6161
buffer.delta_time = time.delta_seconds();
6262
buffer.frame_count = frame_count.0;
6363

0 commit comments

Comments
 (0)