Skip to content

Commit

Permalink
Provide public EntityRef::get_change_ticks_by_id that takes `Compon…
Browse files Browse the repository at this point in the history
…entId` (bevyengine#6683)

# Objective

Fixes bevyengine#6682 

## Solution

Add `EntityRef::get_change_ticks_by_id`
Add `EntityMut::get_change_ticks_by_id`


Co-authored-by: Aleksandr Belkin <sQu1rr@users.noreply.github.com>
  • Loading branch information
2 people authored and ItsDoot committed Feb 1, 2023
1 parent 6b9fa22 commit 56a6b3e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/bevy_ecs/src/world/entity_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ impl<'w> EntityRef<'w> {
unsafe { get_ticks_with_type(self.world, TypeId::of::<T>(), self.entity, self.location) }
}

/// Retrieves the change ticks for the given [`ComponentId`]. This can be useful for implementing change
/// detection in custom runtimes.
///
/// **You should prefer to use the typed API [`EntityRef::get_change_ticks`] where possible and only
/// use this in cases where the actual component types are not known at
/// compile time.**
#[inline]
pub fn get_change_ticks_by_id(&self, component_id: ComponentId) -> Option<ComponentTicks> {
if !self.contains_id(component_id) {
return None;
}

// SAFETY: Entity location is valid and component_id exists.
unsafe { get_ticks(self.world, component_id, self.entity, self.location) }
}

/// Gets a mutable reference to the component of type `T` associated with
/// this entity without ensuring there are no other borrows active and without
/// ensuring that the returned reference will stay valid.
Expand Down Expand Up @@ -206,6 +222,22 @@ impl<'w> EntityMut<'w> {
unsafe { get_ticks_with_type(self.world, TypeId::of::<T>(), self.entity, self.location) }
}

/// Retrieves the change ticks for the given [`ComponentId`]. This can be useful for implementing change
/// detection in custom runtimes.
///
/// **You should prefer to use the typed API [`EntityMut::get_change_ticks`] where possible and only
/// use this in cases where the actual component types are not known at
/// compile time.**
#[inline]
pub fn get_change_ticks_by_id(&self, component_id: ComponentId) -> Option<ComponentTicks> {
if !self.contains_id(component_id) {
return None;
}

// SAFETY: Entity location is valid and component_id exists.
unsafe { get_ticks(self.world, component_id, self.entity, self.location) }
}

/// Gets a mutable reference to the component of type `T` associated with
/// this entity without ensuring there are no other borrows active and without
/// ensuring that the returned reference will stay valid.
Expand Down

0 comments on commit 56a6b3e

Please sign in to comment.