Struct oxygengine_core::ecs::commands::EntityRef
source · 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 satisfies<Q>(&self) -> boolwhere
Q: Query,
pub fn satisfies<Q>(&self) -> boolwhere Q: Query,
Determine whether this entity would satisfy the query Q
sourcepub fn has<T>(&self) -> boolwhere
T: Component,
pub fn has<T>(&self) -> boolwhere T: Component,
Determine whether this entity has a T
component without borrowing it
Equivalent to satisfies::<&T>
sourcepub fn get<T>(&self) -> Option<<T as ComponentRef<'a>>::Ref>where
T: ComponentRef<'a>,
pub fn get<T>(&self) -> Option<<T as ComponentRef<'a>>::Ref>where T: ComponentRef<'a>,
Borrow a single component, if it exists
T
must be a shared or unique reference to a component type.
Example
let mut world = World::new();
let a = world.spawn((42, "abc"));
let e = world.entity(a).unwrap();
*e.get::<&mut i32>().unwrap() = 17;
assert_eq!(*e.get::<&i32>().unwrap(), 17);
Panics if T
is a unique reference and the component is already borrowed, or if the
component is already uniquely borrowed.
sourcepub fn query<Q>(&self) -> QueryOne<'a, Q>where
Q: Query,
pub fn query<Q>(&self) -> QueryOne<'a, Q>where Q: Query,
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.