diff --git a/crates/bevy_ecs/src/reflect/component.rs b/crates/bevy_ecs/src/reflect/component.rs index a6b18839d5..f211597e41 100644 --- a/crates/bevy_ecs/src/reflect/component.rs +++ b/crates/bevy_ecs/src/reflect/component.rs @@ -62,7 +62,10 @@ use crate::{ change_detection::Mut, component::Component, entity::Entity, - world::{unsafe_world_cell::UnsafeEntityCell, EntityMut, EntityRef, EntityWorldMut, World}, + world::{ + unsafe_world_cell::UnsafeEntityCell, EntityMut, EntityWorldMut, FilteredEntityMut, + FilteredEntityRef, World, + }, }; use bevy_reflect::{FromReflect, FromType, Reflect, TypeRegistry}; @@ -104,11 +107,11 @@ pub struct ReflectComponentFns { /// Function pointer implementing [`ReflectComponent::remove()`]. pub remove: fn(&mut EntityWorldMut), /// Function pointer implementing [`ReflectComponent::contains()`]. - pub contains: fn(EntityRef) -> bool, + pub contains: fn(FilteredEntityRef) -> bool, /// Function pointer implementing [`ReflectComponent::reflect()`]. - pub reflect: fn(EntityRef) -> Option<&dyn Reflect>, + pub reflect: fn(FilteredEntityRef) -> Option<&dyn Reflect>, /// Function pointer implementing [`ReflectComponent::reflect_mut()`]. - pub reflect_mut: fn(EntityMut) -> Option>, + pub reflect_mut: fn(FilteredEntityMut) -> Option>, /// Function pointer implementing [`ReflectComponent::reflect_unchecked_mut()`]. /// /// # Safety @@ -165,19 +168,19 @@ impl ReflectComponent { } /// Returns whether entity contains this [`Component`] - pub fn contains(&self, entity: EntityRef) -> bool { - (self.0.contains)(entity) + pub fn contains<'a>(&self, entity: impl Into>) -> bool { + (self.0.contains)(entity.into()) } /// Gets the value of this [`Component`] type from the entity as a reflected reference. - pub fn reflect<'a>(&self, entity: EntityRef<'a>) -> Option<&'a dyn Reflect> { - (self.0.reflect)(entity) + pub fn reflect<'a>(&self, entity: impl Into>) -> Option<&'a dyn Reflect> { + (self.0.reflect)(entity.into()) } /// Gets the value of this [`Component`] type from the entity as a mutable reflected reference. pub fn reflect_mut<'a>( &self, - entity: impl Into>, + entity: impl Into>, ) -> Option> { (self.0.reflect_mut)(entity.into()) } diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index 5e21f6b998..45d90de363 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -2102,6 +2102,19 @@ impl<'w> FilteredEntityMut<'w> { .flatten() } + /// Consumes self and gets mutable access to the component of type `T` + /// with the world `'w` lifetime for the current entity. + /// Returns `None` if the entity does not have a component of type `T`. + #[inline] + pub fn into_mut(self) -> Option> { + let id = self.entity.world().components().get_id(TypeId::of::())?; + self.access + .has_write(id) + // SAFETY: We have write access + .then(|| unsafe { self.entity.get_mut() }) + .flatten() + } + /// Retrieves the change ticks for the given component. This can be useful for implementing change /// detection in custom runtimes. #[inline]