Skip to content

Commit ad1691e

Browse files
authored
Update EntityCommands::trigger to check for the entity's existence (#18071)
## Objective `EntityCommands::trigger` internally uses `Commands::trigger_targets`, which means it gets queued using `Commands::queue` rather `EntityCommands::queue`. This previously wouldn't have made much difference, but now entity commands check whether the entity exists, and that check never happens in this case. ## Solution - Add `entity_command::trigger`, which calls the same function as before (`World::trigger_targets_with_caller`) but through the `EntityWorldMut` passed to entity commands. - Change `EntityCommands::trigger` to queue the new entity command normally.
1 parent 780f658 commit ad1691e

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

crates/bevy_ecs/src/system/commands/entity_command.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,19 @@ pub fn observe<E: Event, B: Bundle, M>(
268268
}
269269
}
270270

271+
/// An [`EntityCommand`] that sends a [`Trigger`](crate::observer::Trigger) targeting an entity.
272+
/// This will run any [`Observer`](crate::observer::Observer) of the given [`Event`] watching the entity.
273+
#[track_caller]
274+
pub fn trigger(event: impl Event) -> impl EntityCommand {
275+
let caller = MaybeLocation::caller();
276+
move |mut entity: EntityWorldMut| {
277+
let id = entity.id();
278+
entity.world_scope(|world| {
279+
world.trigger_targets_with_caller(event, id, caller);
280+
});
281+
}
282+
}
283+
271284
/// An [`EntityCommand`] that clones parts of an entity onto another entity,
272285
/// configured through [`EntityClonerBuilder`].
273286
pub fn clone_with(

crates/bevy_ecs/src/system/commands/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,13 +1916,11 @@ impl<'a> EntityCommands<'a> {
19161916
&mut self.commands
19171917
}
19181918

1919-
/// Sends a [`Trigger`] targeting this entity. This will run any [`Observer`] of the `event` that
1920-
/// watches this entity.
1921-
///
1922-
/// [`Trigger`]: crate::observer::Trigger
1919+
/// Sends a [`Trigger`](crate::observer::Trigger) targeting the entity.
1920+
/// This will run any [`Observer`] of the given [`Event`] watching this entity.
1921+
#[track_caller]
19231922
pub fn trigger(&mut self, event: impl Event) -> &mut Self {
1924-
self.commands.trigger_targets(event, self.entity);
1925-
self
1923+
self.queue(entity_command::trigger(event))
19261924
}
19271925

19281926
/// Creates an [`Observer`] listening for events of type `E` targeting this entity.

0 commit comments

Comments
 (0)