pub trait EntityCommand: Send + 'static {
    // Required method
    fn write(self, id: Entity, world: &mut World);

    // Provided method
    fn with_entity(self, id: Entity) -> WithEntity<Self>
       where Self: Sized { ... }
}
Expand description

A Command which gets executed for a given Entity.

Examples

use bevy_ecs::system::EntityCommand;

#[derive(Resource, Default)]
struct Counter(i64);

/// A `Command` which names an entity based on a global counter.
struct CountName;

impl EntityCommand for CountName {
    fn write(self, id: Entity, world: &mut World) {
        // Get the current value of the counter, and increment it for next time.
        let mut counter = world.resource_mut::<Counter>();
        let i = counter.0;
        counter.0 += 1;

        // Name the entity after the value of the counter.
        world.entity_mut(id).insert(Name::new(format!("Entity #{i}")));
    }
}

// App creation boilerplate omitted...

fn setup(mut commands: Commands) {
    commands.spawn_empty().add(CountName);
    commands.spawn_empty().add(CountName);
}

fn assert_names(named: Query<&Name>) {
    // We use a HashSet because we do not care about the order.
    let names: HashSet<_> = named.iter().map(Name::as_str).collect();
    assert_eq!(names, HashSet::from_iter(["Entity #0", "Entity #1"]));
}

Required Methods§

fn write(self, id: Entity, world: &mut World)

Provided Methods§

fn with_entity(self, id: Entity) -> WithEntity<Self>where Self: Sized,

Returns a Command which executes this EntityCommand for the given Entity.

Implementors§

§

impl<F> EntityCommand for Fwhere F: FnOnce(Entity, &mut World) + Send + 'static,