mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 06:30:19 +00:00
Add panicking helpers for getting components from Query
(#9659)
# Objective - Currently we don't have panicking alternative for getting components from `Query` like for resources. Partially addresses #9443. ## Solution - Add these functions. --- ## Changelog ### Added - `Query::component` and `Query::component_mut` to get specific component from query and panic on error.
This commit is contained in:
parent
a166b65241
commit
9309d89bb8
1 changed files with 51 additions and 1 deletions
|
@ -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<T: 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<T: Component>(
|
||||
|
@ -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<T: Component>(&self, entity: Entity) -> &T {
|
||||
match self.get_component(entity) {
|
||||
Ok(component) => component,
|
||||
Err(error) => {
|
||||
panic!(
|
||||
"Cannot get component `{:?}` from {entity:?}: {error}",
|
||||
TypeId::of::<T>()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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<T: Component>(&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::<T>()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
|
|
Loading…
Reference in a new issue