Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Provide public EntityRef::get_change_ticks_by_id that takes ComponentId #6683

Closed
wants to merge 5 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
sQu1rr marked this conversation as resolved.
Show resolved Hide resolved
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
sQu1rr marked this conversation as resolved.
Show resolved Hide resolved
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