Skip to content

Commit

Permalink
Fix DetectChanges::last_changed returning the wrong tick (bevyengin…
Browse files Browse the repository at this point in the history
…e#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.
  • Loading branch information
chrisjuchem authored and myreprise1 committed Feb 11, 2023
1 parent 2bea41f commit 77333e2
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -150,7 +150,7 @@ macro_rules! change_detection_impl {

#[inline]
fn last_changed(&self) -> u32 {
self.ticks.last_change_tick
self.ticks.changed.tick
}
}

Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 77333e2

Please sign in to comment.