Struct specs::world::LazyUpdate

source ·
pub struct LazyUpdate { /* 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 LazyUpdate mutably. This resource is added to the world by default.

Implementations§

source§

impl LazyUpdate

source

pub fn insert<C>(&self, e: Entity, c: C)where C: Send + Sync + Component,

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, LazyUpdate>);

    fn run(&mut self, (ent, lazy): Self::SystemData) {
        let a = ent.create();
        lazy.insert(a, Pos(1.0, 1.0));
    }
}
source

pub fn insert_all<C, I>(&self, iter: I)where C: Send + Sync + Component, I: Send + Sync + IntoIterator<Item = (Entity, C)> + 'static,

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, LazyUpdate>);

    fn run(&mut self, (ent, lazy): Self::SystemData) {
        let a = ent.create();
        let b = ent.create();

        lazy.insert_all(vec![(a, Pos(3.0, 1.0)), (b, Pos(0.0, 4.0))]);
    }
}
source

pub fn remove<C>(&self, e: Entity)where C: Send + Sync + 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, LazyUpdate>);

    fn run(&mut self, (ent, lazy): Self::SystemData) {
        for entity in ent.join() {
            lazy.remove::<Pos>(entity);
        }
    }
}
source

pub fn exec<F>(&self, f: F)where F: Send + Sync + FnOnce(&mut World) + 'static,

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, LazyUpdate>);

    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);
                }
            });
        }
    }
}
source

pub fn exec_mut<F>(&self, f: F)where F: Send + Sync + FnOnce(&mut World) + 'static,

Lazily executes a closure with mutable world access.

This can be used to add a resource to the World from a system.

Examples

struct Sys;

impl<'a> System<'a> for Sys {
    type SystemData = (Entities<'a>, Read<'a, LazyUpdate>);

    fn run(&mut self, (ent, lazy): Self::SystemData) {
        for entity in ent.join() {
            lazy.exec_mut(move |world| {
                // complete extermination!
                world.delete_all();
            });
        }
    }
}
source

pub fn create_entity(&self, ent: &EntitiesRes) -> LazyBuilder<'_>

Creates a new LazyBuilder which inserts components using LazyUpdate. 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(&entities).with(Pos(1.0, 3.0)).build();

Trait Implementations§

source§

impl Default for LazyUpdate

source§

fn default() -> LazyUpdate

Returns the “default value” for a type. Read more
source§

impl Drop for LazyUpdate

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> TryDefault for Twhere T: Default,

source§

fn try_default() -> Result<T, String>

Tries to create the default.
source§

fn unwrap_default() -> Self

Calls try_default and panics on an error case.
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Event for Twhere T: Send + Sync + 'static,

source§

impl<T> Resource for Twhere T: Any + Send + Sync,