From cdc894a29d37ecb662e8ac1c7a7f04d5a233b263 Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Tue, 8 Mar 2022 18:22:38 -0500 Subject: [PATCH 1/5] Use getter pattern for ChangeTicks system parameter --- crates/bevy_ecs/src/system/system_param.rs | 26 +++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index a5a7cb564637a..e38aabee101bd 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -1164,11 +1164,31 @@ impl<'w, 's> SystemParamFetch<'w, 's> for BundlesState { } } -/// The [`SystemParamState`] of [`SystemChangeTick`]. +/// The looping time-stamp used for change detection +/// +/// This is updated each time the system is run (when it is dispatched by the scheduler): +/// the previous `change_tick` is stored as the `last_change_tick`, +/// and [`World::change_tick`] is used as the new `change_tick`. +/// +/// Changes that occured more recently than the last change tick will be detected by the system. +/// You can check when changes last occured to a piece of data +/// by using [`DetectChanges::change_tick`](crate::change_detection::DetectChanges) on the data. #[derive(Debug)] pub struct SystemChangeTick { - pub last_change_tick: u32, - pub change_tick: u32, + last_change_tick: u32, + change_tick: u32, +} + +impl SystemChangeTick { + /// What is the change tick used by this system? + fn change_tick(&self) -> u32 { + self.change_tick + } + + /// What was the change tick of the [`World`] the last time this system ran? + fn last_change_tick(&self) -> u32 { + self.last_change_tick + } } // SAFE: Only reads internal system state From dcce6c606efebdf0b1131b8832a4f8c373528aa9 Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Tue, 8 Mar 2022 18:28:50 -0500 Subject: [PATCH 2/5] Add last_changed method to DetectChanges trait --- crates/bevy_ecs/src/change_detection.rs | 14 ++++++++++++++ crates/bevy_ecs/src/system/system_param.rs | 6 +++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 369d9ecfab486..45f728f951dac 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -42,6 +42,15 @@ pub trait DetectChanges { /// /// **Note**: This operation is irreversible. fn set_changed(&mut self); + + /// The last time this data was changed, in change ticks + /// + /// Note that data is flagged as changed when it is first added to the [`World`](crate::world::World) as well. + /// + /// This can be compared to the change tick of your systems for debugging purposes + /// using the [`SystemChangeTick`](crate::system::system_param::SystemChangeTick) + /// [`SystemParam`](crate::system::system_param::SystemParam). + fn last_changed(&self) -> u32; } macro_rules! change_detection_impl { @@ -67,6 +76,11 @@ macro_rules! change_detection_impl { .component_ticks .set_changed(self.ticks.change_tick); } + + #[inline] + fn last_changed(&self) -> u32 { + self.ticks.last_change_tick + } } impl<$($generics),* $(: $traits)?> Deref for $name<$($generics),*> { diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index e38aabee101bd..44c815375d702 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -1172,7 +1172,7 @@ impl<'w, 's> SystemParamFetch<'w, 's> for BundlesState { /// /// Changes that occured more recently than the last change tick will be detected by the system. /// You can check when changes last occured to a piece of data -/// by using [`DetectChanges::change_tick`](crate::change_detection::DetectChanges) on the data. +/// by using [`DetectChanges::last_changed`](crate::change_detection::DetectChanges) on the data. #[derive(Debug)] pub struct SystemChangeTick { last_change_tick: u32, @@ -1181,12 +1181,12 @@ pub struct SystemChangeTick { impl SystemChangeTick { /// What is the change tick used by this system? - fn change_tick(&self) -> u32 { + pub fn change_tick(&self) -> u32 { self.change_tick } /// What was the change tick of the [`World`] the last time this system ran? - fn last_change_tick(&self) -> u32 { + pub fn last_change_tick(&self) -> u32 { self.last_change_tick } } From 3953a8fc41df2660d64185dbdb49b6d979cd25b2 Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Tue, 8 Mar 2022 19:47:57 -0500 Subject: [PATCH 3/5] Doc wording nits Co-authored-by: Joy <51241057+maniwani@users.noreply.github.com> --- crates/bevy_ecs/src/change_detection.rs | 8 ++++---- crates/bevy_ecs/src/system/system_param.rs | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 45f728f951dac..9d8142079893b 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -43,12 +43,12 @@ pub trait DetectChanges { /// **Note**: This operation is irreversible. fn set_changed(&mut self); - /// The last time this data was changed, in change ticks + /// Returns the change tick recording the previous time this component (or resource) was changed. /// - /// Note that data is flagged as changed when it is first added to the [`World`](crate::world::World) as well. + /// Note that components and resources are also marked as changed upon insertion. /// - /// This can be compared to the change tick of your systems for debugging purposes - /// using the [`SystemChangeTick`](crate::system::system_param::SystemChangeTick) + /// For comparison, the previous change tick of a system can be read using the + /// [`SystemChangeTick`](crate::system::system_param::SystemChangeTick) /// [`SystemParam`](crate::system::system_param::SystemParam). fn last_changed(&self) -> u32; } diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 44c815375d702..494b9d8691de1 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -1164,15 +1164,15 @@ impl<'w, 's> SystemParamFetch<'w, 's> for BundlesState { } } -/// The looping time-stamp used for change detection +/// A [`SystemParam`] that reads the previous and current change ticks of the system. /// -/// This is updated each time the system is run (when it is dispatched by the scheduler): -/// the previous `change_tick` is stored as the `last_change_tick`, -/// and [`World::change_tick`] is used as the new `change_tick`. +/// A system's change ticks are updated each time it runs: +/// - `last_change_tick` copies the previous value of `change_tick` +/// - `change_tick` copies the current value of [`World::read_change_tick`] /// -/// Changes that occured more recently than the last change tick will be detected by the system. -/// You can check when changes last occured to a piece of data -/// by using [`DetectChanges::last_changed`](crate::change_detection::DetectChanges) on the data. +/// Component change ticks that are more recent than `last_change_tick` will be detected by the system. +/// Those can be read by calling [`last_changed`](crate::change_detection::DetectChanges::last_changed) +/// on a [`Mut`](crate::change_detection::Mut) or [`ResMut`](crate::change_detection::ResMut). #[derive(Debug)] pub struct SystemChangeTick { last_change_tick: u32, @@ -1180,12 +1180,12 @@ pub struct SystemChangeTick { } impl SystemChangeTick { - /// What is the change tick used by this system? + /// Returns the current [`World`] change tick seen by the system. pub fn change_tick(&self) -> u32 { self.change_tick } - /// What was the change tick of the [`World`] the last time this system ran? + /// Returns the [`World`] change tick seen by the system the previous time it ran. pub fn last_change_tick(&self) -> u32 { self.last_change_tick } From 9f02de5dc50c58c41e88ca950415f66a045adde1 Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Tue, 8 Mar 2022 20:04:27 -0500 Subject: [PATCH 4/5] Fix doc links --- crates/bevy_ecs/src/change_detection.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 9d8142079893b..e09e4501dd36d 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -48,8 +48,8 @@ pub trait DetectChanges { /// Note that components and resources are also marked as changed upon insertion. /// /// For comparison, the previous change tick of a system can be read using the - /// [`SystemChangeTick`](crate::system::system_param::SystemChangeTick) - /// [`SystemParam`](crate::system::system_param::SystemParam). + /// [`SystemChangeTick`](crate::system::SystemChangeTick) + /// [`SystemParam`](crate::system::SystemParam). fn last_changed(&self) -> u32; } From ce77cae89d5868db07ed2250b8e328be6469c388 Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Wed, 9 Mar 2022 15:53:57 -0500 Subject: [PATCH 5/5] Inline accessor methods --- crates/bevy_ecs/src/system/system_param.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 494b9d8691de1..e86b0d12abb68 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -1181,11 +1181,13 @@ pub struct SystemChangeTick { impl SystemChangeTick { /// Returns the current [`World`] change tick seen by the system. + #[inline] pub fn change_tick(&self) -> u32 { self.change_tick } /// Returns the [`World`] change tick seen by the system the previous time it ran. + #[inline] pub fn last_change_tick(&self) -> u32 { self.last_change_tick }