pub struct Lazy { /* private fields */ }
Expand description
Lazy updates can be used for world updates that need to borrow a lot of resources
and as such should better be done at the end. They work lazily in the sense that
they are dispatched when calling world.maintain()
.
Lazy updates are dispatched in the order that they are requested. Multiple updates sent from one system may be overridden by updates sent from other systems.
Please note that the provided methods take &self
so there’s no need to get
Lazy
mutably. This resource is added to the world by default.
Implementations§
Source§impl Lazy
impl Lazy
Sourcepub fn exec<F>(&self, f: F)
pub fn exec<F>(&self, f: F)
Lazily executes a closure with world access.
§Examples
struct Pos;
impl Component for Pos {
type Storage = VecStorage<Self>;
}
struct Execution;
impl<'a> System<'a> for Execution {
type SystemData = (Entities<'a>, Read<'a, Lazy>);
fn run(&mut self, (ent, lazy): Self::SystemData) {
for entity in ent.join() {
lazy.exec(move |world| {
if world.is_alive(entity) {
println!("Entity {:?} is alive.", entity);
}
});
}
}
}
Sourcepub fn exec_async<F>(&self, f: F)
pub fn exec_async<F>(&self, f: F)
Same as Lazy::exec
but with async response.
Sourcepub fn insert<C>(&self, e: Entity, c: C)
pub fn insert<C>(&self, e: Entity, c: C)
Lazily inserts a component for an entity.
§Examples
struct Pos(f32, f32);
impl Component for Pos {
type Storage = VecStorage<Self>;
}
struct InsertPos;
impl<'a> System<'a> for InsertPos {
type SystemData = (Entities<'a>, Read<'a, Lazy>);
fn run(&mut self, (ent, lazy): Self::SystemData) {
let a = ent.create();
lazy.insert(a, Pos(1.0, 1.0));
}
}
Sourcepub fn insert_many<C, I>(&self, iter: I)
pub fn insert_many<C, I>(&self, iter: I)
Lazily inserts components for entities.
§Examples
struct Pos(f32, f32);
impl Component for Pos {
type Storage = VecStorage<Self>;
}
struct InsertPos;
impl<'a> System<'a> for InsertPos {
type SystemData = (Entities<'a>, Read<'a, Lazy>);
fn run(&mut self, (ent, lazy): Self::SystemData) {
let a = ent.create();
let b = ent.create();
lazy.insert_many(vec![(a, Pos(3.0, 1.0)), (b, Pos(0.0, 4.0))]);
}
}
Sourcepub fn remove<C>(&self, e: Entity)where
C: Component,
pub fn remove<C>(&self, e: Entity)where
C: Component,
Lazily removes a component.
§Examples
struct Pos;
impl Component for Pos {
type Storage = VecStorage<Self>;
}
struct RemovePos;
impl<'a> System<'a> for RemovePos {
type SystemData = (Entities<'a>, Read<'a, Lazy>);
fn run(&mut self, (ent, lazy): Self::SystemData) {
for entity in ent.join() {
lazy.remove::<Pos>(entity);
}
}
}
Sourcepub fn remove_many<C, I>(&self, iter: I)
pub fn remove_many<C, I>(&self, iter: I)
Lazily removes a component.
§Examples
struct Pos;
impl Component for Pos {
type Storage = VecStorage<Self>;
}
struct RemovePos;
impl<'a> System<'a> for RemovePos {
type SystemData = (Entities<'a>, Read<'a, Lazy>);
fn run(&mut self, (ent, lazy): Self::SystemData) {
for entity in ent.join() {
lazy.remove::<Pos>(entity);
}
}
}
Sourcepub fn create_entity(&self, world: &World) -> LazyBuilder<'_>
pub fn create_entity(&self, world: &World) -> LazyBuilder<'_>
Creates a new LazyBuilder
which inserts components
using Lazy
. This means that the components won’t
be available immediately, but only after a maintain
on World
is performed.
§Examples
struct Pos(f32, f32);
impl Component for Pos {
type Storage = VecStorage<Self>;
}
let my_entity = lazy.create_entity(&world).with(Pos(1.0, 3.0)).build();