Skip to main content

EntityWorldMut

Struct EntityWorldMut 

Source
pub struct EntityWorldMut<'w> { /* private fields */ }
Expand description

A mutable reference to an entity’s data within a World.

EntityWorldMut provides a fluent builder API for constructing entities with components. It holds a mutable borrow of the world, allowing chained component insertions.

§Builder Pattern

The primary use is via World::spawn() for fluent entity construction:

let entity = world.spawn()
    .insert(Position { x: 0.0, y: 0.0 })
    .insert(Velocity { x: 1.0, y: 0.0 })
    .id();

§Lifetime

The builder holds a mutable borrow of the World, so you cannot access the world while an EntityWorldMut exists. Call id() to get the entity ID and release the borrow.

§Thread Safety

EntityWorldMut is not Send or Sync - it’s designed for single-threaded entity construction. For batch spawning, use World::spawn_batch() (future).

Implementations§

Source§

impl<'w> EntityWorldMut<'w>

Source

pub fn id(&self) -> Entity

Returns the Entity ID of the entity being built.

This is commonly used at the end of a builder chain to get the entity ID for later reference.

§Example
let entity = world.spawn().id();
assert!(world.is_alive(entity));
Source

pub fn world(&self) -> &World

Returns a reference to the World containing this entity.

This allows read-only access to world state while building an entity. For mutable access, you’ll need to finish building and drop this EntityWorldMut first.

Source

pub fn world_mut(&mut self) -> &mut World

Returns a mutable reference to the World containing this entity.

§Warning

Be careful when accessing the world mutably - ensure you don’t invalidate this entity or its archetype in unexpected ways.

Source

pub fn insert<T: Component>(&mut self, component: T) -> &mut Self

Inserts a component on this entity.

If the entity already has a component of this type, it is replaced. Returns self for method chaining.

§Type Parameters
  • T: The component type to insert
§Arguments
  • component - The component value to insert
§Returns

&mut Self for fluent method chaining.

§Example
use goud_engine::ecs::{World, Component};

#[derive(Debug, Clone, Copy, PartialEq)]
struct Position { x: f32, y: f32 }
impl Component for Position {}

#[derive(Debug, Clone, Copy, PartialEq)]
struct Velocity { x: f32, y: f32 }
impl Component for Velocity {}

let mut world = World::new();

// Fluent builder pattern
let entity = world.spawn()
    .insert(Position { x: 0.0, y: 0.0 })
    .insert(Velocity { x: 1.0, y: 0.0 })
    .id();

assert!(world.has::<Position>(entity));
assert!(world.has::<Velocity>(entity));
assert_eq!(world.get::<Position>(entity), Some(&Position { x: 0.0, y: 0.0 }));
assert_eq!(world.get::<Velocity>(entity), Some(&Velocity { x: 1.0, y: 0.0 }));
§Archetype Transitions

Each insert call may trigger an archetype transition if the entity doesn’t already have the component type. Multiple inserts in sequence will create intermediate archetypes.

Trait Implementations§

Source§

impl<'w> Debug for EntityWorldMut<'w>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'w> Freeze for EntityWorldMut<'w>

§

impl<'w> !RefUnwindSafe for EntityWorldMut<'w>

§

impl<'w> !Send for EntityWorldMut<'w>

§

impl<'w> !Sync for EntityWorldMut<'w>

§

impl<'w> Unpin for EntityWorldMut<'w>

§

impl<'w> UnsafeUnpin for EntityWorldMut<'w>

§

impl<'w> !UnwindSafe for EntityWorldMut<'w>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T, U> Into<U> for T
where 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.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,