pub struct EntityCommands<'a> { /* private fields */ }Expand description
Commands on one entity: an existing one (Commands::entity) or one being spawned
(Commands::spawn). The methods chain: commands.spawn()?.insert(a)?.insert(b)?.
use pragma_ecs::{Commands, EcsError};
struct Position(f32, f32);
struct Bullet { damage: u32 }
fn fire(mut commands: Commands) -> Result<(), EcsError> {
commands
.spawn()?
.insert(Position(0.0, 0.0))?
.insert(Bullet { damage: 3 })?;
Ok(())
}Implementations§
Source§impl EntityCommands<'_>
impl EntityCommands<'_>
Sourcepub const fn id(&self) -> Option<Entity>
pub const fn id(&self) -> Option<Entity>
The entity, if it existed when the commands were recorded; None for a spawned entity,
whose id is allocated when the commands are applied.
Sourcepub fn insert<T: Component>(&mut self, value: T) -> Result<&mut Self, EcsError>
pub fn insert<T: Component>(&mut self, value: T) -> Result<&mut Self, EcsError>
Records inserting value (or replacing the component of that type).
§Errors
EcsError::OutOfMemory if the queue cannot grow; value is dropped.
Sourcepub fn insert_bundle<B: Bundle>(
&mut self,
bundle: B,
) -> Result<&mut Self, EcsError>
pub fn insert_bundle<B: Bundle>( &mut self, bundle: B, ) -> Result<&mut Self, EcsError>
Records inserting the components of bundle in one table move (or replacing those of
the same types).
use pragma_ecs::{Commands, EcsError};
struct Name(&'static str);
struct Level(u32);
struct Gold(u32);
fn hire(mut commands: Commands) -> Result<(), EcsError> {
commands.spawn()?.insert(Name("squire"))?.insert_bundle((Level(1), Gold(10)))?;
Ok(())
}§Errors
EcsError::DuplicateComponent if the bundle holds a component type twice,
EcsError::OutOfMemory if the queue cannot grow; bundle is dropped.
Sourcepub fn despawn(self) -> Result<(), EcsError>
pub fn despawn(self) -> Result<(), EcsError>
Records despawning the entity; ends the chain. A spawned entity that is despawned in the same queue is created and destroyed at the sync point, its id freed again.
use pragma_ecs::{Commands, EcsError, Entity, View, With};
struct Ghost;
struct Haunting(u32);
fn exorcise(mut ghosts: View<Entity, With<Ghost>>, mut commands: Commands) -> Result<(), EcsError> {
for entity in ghosts.iter()? {
commands.entity(entity).despawn()?;
}
// Created and destroyed at the sync point: the entity never shows up in a query.
let mut probe = commands.spawn()?;
probe.insert(Haunting(0))?.remove::<Haunting>()?;
probe.despawn()
}§Errors
EcsError::OutOfMemory if the queue cannot grow.