Struct CommandBuffer

Source
pub struct CommandBuffer { /* private fields */ }
Expand description

A buffer of deferred ECS commands such as entity creation, component addition/removal, and destruction.

This is useful for queuing up changes during system execution to avoid mutably borrowing the world while it’s being accessed elsewhere. Changes can be applied all at once using CommandBuffer::execute.

§Fields

  • commands: The queued operations to apply.

§Safety

  • Commands are applied with exclusive access to the World to ensure memory safety.

Implementations§

Source§

impl CommandBuffer

Source

pub fn clear(&mut self)

Clears the buffer

Source

pub fn create_entity(&mut self)

Queues a command to create a new entity in the world. The entity will be returned by execute when the command is applied.

Source

pub fn destroy_entity(&mut self, entity: Entity)

Queues a command to destroy an entity. This will remove all of its components and make its ID reusable.

§Parameters
  • entity: The entity to be destroyed.
Source

pub fn add_component<T: Component + Send + 'static>( &mut self, entity: Entity, component: T, )

Queues a command to add a component to an entity. The command is deferred to avoid borrowing issues during system execution.

§Type Parameters
  • T: The component type.
§Parameters
  • entity: The entity to receive the component.
  • component: The component instance to be added.
Source

pub fn remove_component<T: Component + 'static>(&mut self, entity: Entity)

Queues a command to remove a component of type T from an entity.

§Type Parameters
  • T: The component type to remove.
§Parameters
  • entity: The entity from which to remove the component.
Source

pub fn create_entity_and_add_component<T: Component + Send + 'static>( &mut self, component: T, )

Queues a command to create a new entity and immediately add a component to it.

§Type Parameters
  • T: The component type.
§Parameters
  • component: The component instance to be added to the new entity.
Source

pub fn execute(&mut self, world: &mut World) -> Vec<Entity>

Executes all queued commands in order, applying them to the world.

Returns a vector of entities created via create_entity for further tracking.

§Parameters
  • world: The world to apply the commands to.

Trait Implementations§

Source§

impl Default for CommandBuffer

Source§

fn default() -> CommandBuffer

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

Auto Trait Implementations§

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