From fefe5297ad00f93cc92df61153cb15dad3ae4857 Mon Sep 17 00:00:00 2001 From: Chris Juchem Date: Thu, 9 Feb 2023 17:07:15 +0000 Subject: [PATCH] Fix `DetectChanges::last_changed` returning the wrong tick (#7560) # Objective Make `last_changed` behave as described in its docs. ## Solution - Return `changed` instead of `last_change_tick`. `last_change_tick` is the system's previous tick and is just used for comparison. - Update the docs of the similarly named `set_last_changed` (which does correctly interact with `last_change_tick`) to clarify that the two functions touch different data. (I advocate for renaming one or the other if anyone has any good suggestions). It also might make sense to return a cloned `Tick` instead of `u32`. --- ## Changelog - Fixed `DetectChanges::last_changed` returning the wrong value. - Fixed `DetectChangesMut::set_last_changed` not actually updating the `changed` tick. ## Migration Guide - The incorrect value that was being returned by `DetectChanges::last_changed` was the previous run tick of the system checking for changed values. If you depended on this value, you can get it from the `SystemChangeTick` `SystemParam` instead. --- crates/bevy_ecs/src/change_detection.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 609ca15c01..e54ea3d48b 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -49,7 +49,7 @@ pub trait DetectChanges { /// Returns `true` if this value was added or mutably dereferenced after the system last ran. fn is_changed(&self) -> bool; - /// Returns the change tick recording the previous time this data was changed. + /// Returns the change tick recording the time this data was most recently changed. /// /// Note that components and resources are also marked as changed upon insertion. /// @@ -103,7 +103,7 @@ pub trait DetectChangesMut: DetectChanges { /// **Note**: This operation cannot be undone. fn set_changed(&mut self); - /// Manually sets the change tick recording the previous time this data was mutated. + /// Manually sets the change tick recording the time when this data was last mutated. /// /// # Warning /// This is a complex and error-prone operation, primarily intended for use with rollback networking strategies. @@ -150,7 +150,7 @@ macro_rules! change_detection_impl { #[inline] fn last_changed(&self) -> u32 { - self.ticks.last_change_tick + self.ticks.changed.tick } } @@ -186,7 +186,9 @@ macro_rules! change_detection_mut_impl { #[inline] fn set_last_changed(&mut self, last_change_tick: u32) { - self.ticks.last_change_tick = last_change_tick + self.ticks + .changed + .set_changed(last_change_tick); } #[inline]