Skip to content

Commit d3f5d30

Browse files
alice-i-cecileItsDoot
authored andcommitted
Improve debugging tools for change detection (bevyengine#4160)
# Objective 1. Previously, the `change_tick` and `last_change_tick` fields on `SystemChangeTick` [were `pub`](https://docs.rs/bevy/0.6.1/bevy/ecs/system/struct.SystemChangeTick.html). 1. This was actively misleading, as while this can be fetched as a `SystemParam`, a copy is returned instead 2. This information could be useful for debugging, but there was no way to investigate when data was changed. 3. There were no docs! ## Solution 1. Move these to a getter method. 2. Add `last_changed` method to the `DetectChanges` trait to enable inspection of when data was last changed. 3. Add docs. # Changelog `SystemChangeTick` now provides getter methods for the current and previous change tick, rather than public fields. This can be combined with `DetectChanges::last_changed()` to debug the timing of changes. # Migration guide The `change_tick` and `last_change_tick` fields on `SystemChangeTick` are now private, use the corresponding getter method instead.
1 parent 98ca3d3 commit d3f5d30

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

crates/bevy_ecs/src/change_detection.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ pub trait DetectChanges {
4242
///
4343
/// **Note**: This operation is irreversible.
4444
fn set_changed(&mut self);
45+
46+
/// Returns the change tick recording the previous time this component (or resource) was changed.
47+
///
48+
/// Note that components and resources are also marked as changed upon insertion.
49+
///
50+
/// For comparison, the previous change tick of a system can be read using the
51+
/// [`SystemChangeTick`](crate::system::SystemChangeTick)
52+
/// [`SystemParam`](crate::system::SystemParam).
53+
fn last_changed(&self) -> u32;
4554
}
4655

4756
macro_rules! change_detection_impl {
@@ -67,6 +76,11 @@ macro_rules! change_detection_impl {
6776
.component_ticks
6877
.set_changed(self.ticks.change_tick);
6978
}
79+
80+
#[inline]
81+
fn last_changed(&self) -> u32 {
82+
self.ticks.last_change_tick
83+
}
7084
}
7185

7286
impl<$($generics),* $(: $traits)?> Deref for $name<$($generics),*> {

crates/bevy_ecs/src/system/system_param.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,11 +1164,33 @@ impl<'w, 's> SystemParamFetch<'w, 's> for BundlesState {
11641164
}
11651165
}
11661166

1167-
/// The [`SystemParamState`] of [`SystemChangeTick`].
1167+
/// A [`SystemParam`] that reads the previous and current change ticks of the system.
1168+
///
1169+
/// A system's change ticks are updated each time it runs:
1170+
/// - `last_change_tick` copies the previous value of `change_tick`
1171+
/// - `change_tick` copies the current value of [`World::read_change_tick`]
1172+
///
1173+
/// Component change ticks that are more recent than `last_change_tick` will be detected by the system.
1174+
/// Those can be read by calling [`last_changed`](crate::change_detection::DetectChanges::last_changed)
1175+
/// on a [`Mut<T>`](crate::change_detection::Mut) or [`ResMut<T>`](crate::change_detection::ResMut).
11681176
#[derive(Debug)]
11691177
pub struct SystemChangeTick {
1170-
pub last_change_tick: u32,
1171-
pub change_tick: u32,
1178+
last_change_tick: u32,
1179+
change_tick: u32,
1180+
}
1181+
1182+
impl SystemChangeTick {
1183+
/// Returns the current [`World`] change tick seen by the system.
1184+
#[inline]
1185+
pub fn change_tick(&self) -> u32 {
1186+
self.change_tick
1187+
}
1188+
1189+
/// Returns the [`World`] change tick seen by the system the previous time it ran.
1190+
#[inline]
1191+
pub fn last_change_tick(&self) -> u32 {
1192+
self.last_change_tick
1193+
}
11721194
}
11731195

11741196
// SAFE: Only reads internal system state

0 commit comments

Comments
 (0)