pub struct Entity { /* private fields */ }Expand description
Components associated by a common identifier.
Entities are mostly just usize identifiers that help locate components,
but Entity comes with some extra conveniences.
After an entity is created you can attach and remove bundles of components asynchronously (or “lazily” if not in an async context).
use std::sync::Arc;
use apecs::*;
let mut world = World::default();
let mut facade = world.facade();
// here we use `smol` but `apecs` works with any async runtime
// asynchronously create new entities from within a future
let task = smol::spawn(async move {
let mut b = facade
.visit(|mut ents: ViewMut<Entities>| {
ents.create().with_bundle((123, "entity B", true))
})
.await
.unwrap();
// after this await, `b` has its bundle
b.updates().await;
// visit a component or bundle
let did_visit = b
.visit::<&&str, ()>(|name| assert_eq!("entity B", *name.value()))
.await
.is_some();
assert!(did_visit);
});
while !task.is_finished() {
// tick the world
world.tick().unwrap();
// send resources to the facade
world.get_facade_schedule().unwrap().run().unwrap();
}Alternatively, if you have access to the Components resource
you can attach components directly and immediately.
Implementations§
Source§impl Entity
impl Entity
pub fn id(&self) -> usize
Sourcepub fn with_bundle<B: IsBundle + Send + Sync + 'static>(self, bundle: B) -> Self
pub fn with_bundle<B: IsBundle + Send + Sync + 'static>(self, bundle: B) -> Self
Lazily add a component bundle to archetype storage.
This entity will have the associated components after the next tick.
Sourcepub fn insert_bundle<B: IsBundle + Send + Sync + 'static>(&mut self, bundle: B)
pub fn insert_bundle<B: IsBundle + Send + Sync + 'static>(&mut self, bundle: B)
Lazily add a component bundle to archetype storage.
This entity will have the associated components after the next tick.
Sourcepub fn insert_component<T: Send + Sync + 'static>(&mut self, component: T)
pub fn insert_component<T: Send + Sync + 'static>(&mut self, component: T)
Lazily add a component bundle to archetype storage.
This entity will have the associated component after the next tick.
Sourcepub fn remove_component<T: Send + Sync + 'static>(&mut self)
pub fn remove_component<T: Send + Sync + 'static>(&mut self)
Lazily remove a component bundle from archetype storage.
This entity will have lost the associated component after the next tick.
Trait Implementations§
Source§impl Clone for Entity
You may clone entities, but each one does its own lazy updates,
separate from all other clones.
impl Clone for Entity
You may clone entities, but each one does its own lazy updates, separate from all other clones.
Auto Trait Implementations§
impl Freeze for Entity
impl RefUnwindSafe for Entity
impl Send for Entity
impl Sync for Entity
impl Unpin for Entity
impl UnwindSafe for Entity
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more