Struct specs::world::EntityBuilder

source ·
pub struct EntityBuilder<'a> {
    pub entity: Entity,
    pub world: &'a World,
    /* private fields */
}
Expand description

The entity builder, allowing to build an entity together with its components.

Examples

use specs::{prelude::*, storage::HashMapStorage};

struct Health(f32);

impl Component for Health {
    type Storage = HashMapStorage<Self>;
}

struct Pos {
    x: f32,
    y: f32,
}

impl Component for Pos {
    type Storage = DenseVecStorage<Self>;
}

let mut world = World::new();
world.register::<Health>();
world.register::<Pos>();

let entity = world
    .create_entity() // This call returns `EntityBuilder`
    .with(Health(4.0))
    .with(Pos { x: 1.0, y: 3.0 })
    .build(); // Returns the `Entity`

Distinguishing Mandatory Components from Optional Components

use specs::{prelude::*, storage::HashMapStorage};

struct MandatoryHealth(f32);

impl Component for MandatoryHealth {
    type Storage = HashMapStorage<Self>;
}

struct OptionalPos {
    x: f32,
    y: f32,
}

impl Component for OptionalPos {
    type Storage = DenseVecStorage<Self>;
}

let mut world = World::new();
world.register::<MandatoryHealth>();
world.register::<OptionalPos>();

let mut entitybuilder = world.create_entity().with(MandatoryHealth(4.0));

// something trivial to serve as our conditional
let include_optional = true;

if include_optional == true {
    entitybuilder = entitybuilder.with(OptionalPos { x: 1.0, y: 3.0 })
}

let entity = entitybuilder.build();

Fields§

§entity: Entity

The (already created) entity for which components will be inserted.

§world: &'a World

A reference to the World for component insertions.

Trait Implementations§

source§

impl<'a> Builder for EntityBuilder<'a>

source§

fn with<T: Component>(self, c: T) -> Self

Inserts a component for this entity.

If a component was already associated with the entity, it will overwrite the previous component.

source§

fn build(self) -> Entity

Finishes the building and returns the entity. As opposed to LazyBuilder, the components are available immediately.

source§

fn maybe_with<C: Component + Send + Sync>(self, c: Option<C>) -> Selfwhere Self: Sized,

Convenience method that calls self.with(component) if Some(component) is provided Read more
source§

impl<'a> Drop for EntityBuilder<'a>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a> MarkedBuilder for EntityBuilder<'a>

source§

fn marked<M>(self) -> Selfwhere M: Marker,

Add a Marker to the entity by fetching the associated allocator. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for EntityBuilder<'a>

§

impl<'a> Send for EntityBuilder<'a>

§

impl<'a> Sync for EntityBuilder<'a>

§

impl<'a> Unpin for EntityBuilder<'a>

§

impl<'a> !UnwindSafe for EntityBuilder<'a>

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, 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,