diff --git a/crates/bevy_ecs/src/system/query.rs b/crates/bevy_ecs/src/system/query.rs index 9fbad21a291ce..a96bdb3531e79 100644 --- a/crates/bevy_ecs/src/system/query.rs +++ b/crates/bevy_ecs/src/system/query.rs @@ -1091,6 +1091,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { /// /// # See also /// + /// - [`component`](Self::component) a panicking version of this function. /// - [`get_component_mut`](Self::get_component_mut) to get a mutable reference of a component. #[inline] pub fn get_component(&self, entity: Entity) -> Result<&T, QueryComponentError> { @@ -1121,7 +1122,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { /// Returns a mutable reference to the component `T` of the given entity. /// - /// In case of a nonexisting entity or mismatched component, a [`QueryComponentError`] is returned instead. + /// In case of a nonexisting entity, mismatched component or missing write acess, a [`QueryComponentError`] is returned instead. /// /// # Example /// @@ -1145,6 +1146,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { /// /// # See also /// + /// - [`component_mut`](Self::component_mut) a panicking version of this function. /// - [`get_component`](Self::get_component) to get a shared reference of a component. #[inline] pub fn get_component_mut( @@ -1155,6 +1157,54 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { unsafe { self.get_component_unchecked_mut(entity) } } + /// Returns a shared reference to the component `T` of the given [`Entity`]. + /// + /// # Panics + /// + /// Panics in case of a nonexisting entity or mismatched component. + /// + /// # See also + /// + /// - [`get_component`](Self::get_component) a non-panicking version of this function. + /// - [`component_mut`](Self::component_mut) to get a mutable reference of a component. + #[inline] + #[track_caller] + pub fn component(&self, entity: Entity) -> &T { + match self.get_component(entity) { + Ok(component) => component, + Err(error) => { + panic!( + "Cannot get component `{:?}` from {entity:?}: {error}", + TypeId::of::() + ) + } + } + } + + /// Returns a mutable reference to the component `T` of the given entity. + /// + /// # Panics + /// + /// Panics in case of a nonexisting entity, mismatched component or missing write access. + /// + /// # See also + /// + /// - [`get_component_mut`](Self::get_component_mut) a non-panicking version of this function. + /// - [`component`](Self::component) to get a shared reference of a component. + #[inline] + #[track_caller] + pub fn component_mut(&mut self, entity: Entity) -> Mut<'_, T> { + match self.get_component_mut(entity) { + Ok(component) => component, + Err(error) => { + panic!( + "Cannot get component `{:?}` from {entity:?}: {error}", + TypeId::of::() + ) + } + } + } + /// Returns a mutable reference to the component `T` of the given entity. /// /// In case of a nonexisting entity or mismatched component, a [`QueryComponentError`] is returned instead.