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>
impl<'a> Builder for EntityBuilder<'a>
Source§fn with<T: Component>(self, c: T) -> Self
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§impl<'a> Drop for EntityBuilder<'a>
impl<'a> Drop for EntityBuilder<'a>
Source§impl<'a> MarkedBuilder for EntityBuilder<'a>
impl<'a> MarkedBuilder for EntityBuilder<'a>
Auto Trait Implementations§
impl<'a> Freeze for EntityBuilder<'a>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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