From f07bb3c4494d28659bbcfe0726fb909072901e1e Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Mon, 12 Jun 2023 19:55:09 +0200 Subject: [PATCH] Add last_changed_tick and added_tick to ComponentTicks (#8803) # Objective EntityRef::get_change_ticks mentions that ComponentTicks is useful to create change detection for your own runtime. However, ComponentTicks doesn't even expose enough data to create something that implements DetectChanges. Specifically, we need to be able to extract the last change tick. ## Solution We add a method to get the last change tick. We also add a method to get the added tick. ## Changelog - Add `last_changed_tick` and `added_tick` to `ComponentTicks` --- crates/bevy_ecs/src/component.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index 0183f34907..27917d5fd6 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -744,18 +744,30 @@ pub struct ComponentTicks { } impl ComponentTicks { - #[inline] /// Returns `true` if the component was added after the system last ran. + #[inline] pub fn is_added(&self, last_run: Tick, this_run: Tick) -> bool { self.added.is_newer_than(last_run, this_run) } - #[inline] /// Returns `true` if the component was added or mutably dereferenced after the system last ran. + #[inline] pub fn is_changed(&self, last_run: Tick, this_run: Tick) -> bool { self.changed.is_newer_than(last_run, this_run) } + /// Returns the tick recording the time this component was most recently changed. + #[inline] + pub fn last_changed_tick(&self) -> Tick { + self.changed + } + + /// Returns the tick recording the time this component was added. + #[inline] + pub fn added_tick(&self) -> Tick { + self.added + } + pub(crate) fn new(change_tick: Tick) -> Self { Self { added: change_tick,