Skip to content

Commit 9b72780

Browse files
committed
Provide public EntityRef::get_change_ticks_by_id that takes ComponentId (#6683)
# Objective Fixes #6682 ## Solution Add `EntityRef::get_change_ticks_by_id` Add `EntityMut::get_change_ticks_by_id` Co-authored-by: Aleksandr Belkin <[email protected]>
1 parent e89b043 commit 9b72780

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

crates/bevy_ecs/src/world/entity_ref.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ impl<'w> EntityRef<'w> {
8282
unsafe { get_ticks_with_type(self.world, TypeId::of::<T>(), self.entity, self.location) }
8383
}
8484

85+
/// Retrieves the change ticks for the given [`ComponentId`]. This can be useful for implementing change
86+
/// detection in custom runtimes.
87+
///
88+
/// **You should prefer to use the typed API [`EntityRef::get_change_ticks`] where possible and only
89+
/// use this in cases where the actual component types are not known at
90+
/// compile time.**
91+
#[inline]
92+
pub fn get_change_ticks_by_id(&self, component_id: ComponentId) -> Option<ComponentTicks> {
93+
if !self.contains_id(component_id) {
94+
return None;
95+
}
96+
97+
// SAFETY: Entity location is valid and component_id exists.
98+
unsafe { get_ticks(self.world, component_id, self.entity, self.location) }
99+
}
100+
85101
/// Gets a mutable reference to the component of type `T` associated with
86102
/// this entity without ensuring there are no other borrows active and without
87103
/// ensuring that the returned reference will stay valid.
@@ -206,6 +222,22 @@ impl<'w> EntityMut<'w> {
206222
unsafe { get_ticks_with_type(self.world, TypeId::of::<T>(), self.entity, self.location) }
207223
}
208224

225+
/// Retrieves the change ticks for the given [`ComponentId`]. This can be useful for implementing change
226+
/// detection in custom runtimes.
227+
///
228+
/// **You should prefer to use the typed API [`EntityMut::get_change_ticks`] where possible and only
229+
/// use this in cases where the actual component types are not known at
230+
/// compile time.**
231+
#[inline]
232+
pub fn get_change_ticks_by_id(&self, component_id: ComponentId) -> Option<ComponentTicks> {
233+
if !self.contains_id(component_id) {
234+
return None;
235+
}
236+
237+
// SAFETY: Entity location is valid and component_id exists.
238+
unsafe { get_ticks(self.world, component_id, self.entity, self.location) }
239+
}
240+
209241
/// Gets a mutable reference to the component of type `T` associated with
210242
/// this entity without ensuring there are no other borrows active and without
211243
/// ensuring that the returned reference will stay valid.

0 commit comments

Comments
 (0)