Skip to content

Commit 1d3ae67

Browse files
Add discard_overstep function to Time<Fixed> (#10453)
# Objective There is no easy way to discard some amount for `Time<Fixed>`'s overstep. This can be useful for online games when the client receives information about a tick (which happens when you get a FPS drop or the ping changes for example) it has not yet processed, it can discard overstep equal to the number of ticks it can jump ahead. Currently the workaround would be to create a new `Time<Fixed>` copy the old timestep, advance it by the overstep amount that would remain after subtracting the discarded amount, and using `.context_mut()` to overwrite the old context with the new one. If you overwrite the whole `Time<Fixed>` or forget to copy over the timestep you can introduce undesirable side effects. ## Solution Introduce a `discard_overstep` method, which discards the provided amount of overstep. It uses satuarting_sub to avoid errors (negative `Duration`s do not exist). --- ## Changelog - Added `discard_overstep` function to `Time<Fixed>` --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent 7ee9f8e commit 1d3ae67

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

crates/bevy_time/src/fixed.rs

+9
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ impl Time<Fixed> {
175175
self.context().overstep
176176
}
177177

178+
/// Discard a part of the overstep amount.
179+
///
180+
/// If `discard` is higher than overstep, the overstep becomes zero.
181+
#[inline]
182+
pub fn discard_overstep(&mut self, discard: Duration) {
183+
let context = self.context_mut();
184+
context.overstep = context.overstep.saturating_sub(discard);
185+
}
186+
178187
/// Returns the amount of overstep time accumulated toward new steps, as an
179188
/// [`f32`] fraction of the timestep.
180189
#[inline]

0 commit comments

Comments
 (0)