From 1d3ae677df55c3de6953c4b8b5e27a85aa85118b Mon Sep 17 00:00:00 2001 From: NiseVoid Date: Sat, 18 Nov 2023 01:50:26 +0100 Subject: [PATCH] Add discard_overstep function to Time (#10453) # Objective There is no easy way to discard some amount for `Time`'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` 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` 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` --------- Co-authored-by: Alice Cecile --- crates/bevy_time/src/fixed.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/bevy_time/src/fixed.rs b/crates/bevy_time/src/fixed.rs index ced8e821ce9fe..faef1d7d38839 100644 --- a/crates/bevy_time/src/fixed.rs +++ b/crates/bevy_time/src/fixed.rs @@ -175,6 +175,15 @@ impl Time { self.context().overstep } + /// Discard a part of the overstep amount. + /// + /// If `discard` is higher than overstep, the overstep becomes zero. + #[inline] + pub fn discard_overstep(&mut self, discard: Duration) { + let context = self.context_mut(); + context.overstep = context.overstep.saturating_sub(discard); + } + /// Returns the amount of overstep time accumulated toward new steps, as an /// [`f32`] fraction of the timestep. #[inline]