diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 875e0ae9de..3e62310e0d 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -777,7 +777,7 @@ impl<'w, 's> Commands<'w, 's> { self.add(TriggerEvent { event, targets }); } - /// Spawn an [`Observer`] and returns the [`EntityCommands`] associated with the entity that stores the observer. + /// Spawns an [`Observer`] and returns the [`EntityCommands`] associated with the entity that stores the observer. pub fn observe( &mut self, observer: impl IntoObserverSystem, @@ -1210,6 +1210,15 @@ impl EntityCommands<'_> { self.commands.reborrow() } + /// Sends a [`Trigger`] targeting this entity. This will run any [`Observer`] of the `event` that + /// watches this entity. + /// + /// [`Trigger`]: crate::observer::Trigger + pub fn trigger(&mut self, event: impl Event) -> &mut Self { + self.commands.trigger_targets(event, self.entity); + self + } + /// Creates an [`Observer`] listening for a trigger of type `T` that targets this entity. pub fn observe( &mut self, diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index 34dc767341..fa653aaa83 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -1443,6 +1443,12 @@ impl<'w> EntityWorldMut<'w> { } } + /// Triggers the given `event` for this entity, which will run any observers watching for it. + pub fn trigger(&mut self, event: impl Event) -> &mut Self { + self.world.trigger_targets(event, self.entity); + self + } + /// Creates an [`Observer`] listening for events of type `E` targeting this entity. /// In order to trigger the callback the entity must also match the query when the event is fired. pub fn observe(