pub struct EntityRef<'a> { /* private fields */ }Expand description
Handle to an entity with any component types
Implementations§
Source§impl<'a> EntityRef<'a>
impl<'a> EntityRef<'a>
Sourcepub fn has<T: Component>(&self) -> bool
pub fn has<T: Component>(&self) -> bool
Determine whether this entity has a T component without borrowing it
Sourcepub fn get<T: Component>(&self) -> Option<Ref<'a, T>>
pub fn get<T: Component>(&self) -> Option<Ref<'a, T>>
Borrow the component of type T, if it exists
Panics if the component is already uniquely borrowed from another entity with the same components.
Sourcepub fn get_mut<T: Component>(&self) -> Option<RefMut<'a, T>>
pub fn get_mut<T: Component>(&self) -> Option<RefMut<'a, T>>
Uniquely borrow the component of type T, if it exists
Panics if the component is already borrowed from another entity with the same components.
Sourcepub fn query<Q: Query>(&self) -> QueryOne<'a, Q>
pub fn query<Q: Query>(&self) -> QueryOne<'a, Q>
Run a query against this entity
Equivalent to invoking World::query_one on the entity. May
outlive self.
§Example
let mut world = World::new();
let a = world.spawn((123, true, "abc"));
// The returned query must outlive the borrow made by `get`
let mut query = world.entity(a).unwrap().query::<(&mut i32, &bool)>();
let (number, flag) = query.get().unwrap();
if *flag { *number *= 2; }
assert_eq!(*number, 246);Sourcepub fn component_types(&self) -> impl Iterator<Item = TypeId> + 'a
pub fn component_types(&self) -> impl Iterator<Item = TypeId> + 'a
Enumerate the types of the entity’s components
Convenient for dispatching component-specific logic for a single entity. For example, this
can be combined with a HashMap<TypeId, Box<dyn Handler>> where Handler is some
user-defined trait with methods for serialization, or to be called after spawning or before
despawning to maintain secondary indices.