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.
This commit is contained in:
Chris Juchem 2023-02-09 17:07:15 +00:00
parent dd4299bcf9
commit fefe5297ad

View File

@ -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]