Trait EntityCommand

Source
pub trait EntityCommand<Out = ()>: Send + 'static {
    // Required method
    fn apply(self, entity: EntityWorldMut<'_>) -> Out;
}
Expand description

A command which gets executed for a given Entity.

Should be used with EntityCommands::queue.

The Out generic parameter is the returned “output” of the command.

§Examples

use bevy_ecs::system::EntityCommand;

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

/// A `Command` which names an entity based on a global counter.
fn count_name(mut entity: EntityWorldMut) {
    // Get the current value of the counter, and increment it for next time.
    let i = {
        let mut counter = entity.resource_mut::<Counter>();
        let i = counter.0;
        counter.0 += 1;
        i
    };
    // Name the entity after the value of the counter.
    entity.insert(Name::new(format!("Entity #{i}")));
}

// App creation boilerplate omitted...

fn setup(mut commands: Commands) {
    commands.spawn_empty().queue(count_name);
    commands.spawn_empty().queue(count_name);
}

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§

Source

fn apply(self, entity: EntityWorldMut<'_>) -> Out

Executes this command for the given Entity.

Implementors§

Source§

impl<Out, F> EntityCommand<Out> for F
where F: FnOnce(EntityWorldMut<'_>) -> Out + Send + 'static,