Skip to main content

EntityCommand

Trait EntityCommand 

Source
pub trait EntityCommand: Send + 'static {
    type Out: EntityCommandOutput;

    // Required method
    fn apply(self, entity: EntityWorldMut<'_>) -> Self::Out;

    // Provided method
    fn with_entity(self, entity: Entity) -> impl Command
       where Self: Sized { ... }
}
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 Associated Types§

Source

type Out: EntityCommandOutput

The return type of apply.

Required Methods§

Source

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

Executes this command for the given Entity.

Provided Methods§

Source

fn with_entity(self, entity: Entity) -> impl Command
where Self: Sized,

Passes in a specific entity to an EntityCommand, resulting in a Command that internally runs the EntityCommand on that entity.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<Out, F> EntityCommand for Arc<F>
where F: Fn(EntityWorldMut<'_>) -> Out + Send + Sync + ?Sized + 'static, Out: EntityCommandOutput + 'static,

Source§

type Out = Out

Source§

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

Implementors§

Source§

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

Source§

type Out = Out