Skip to content

Commit 85e41dd

Browse files
Add observer to Trigger (#15066)
# Objective - Fixes #15061 ## Solution - Added `observer` to `Trigger`, which returns the entity observing the triggered event. ## Testing - CI passed locally.
1 parent eaa87b8 commit 85e41dd

File tree

1 file changed

+27
-1
lines changed
  • crates/bevy_ecs/src/observer

1 file changed

+27
-1
lines changed

crates/bevy_ecs/src/observer/mod.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,37 @@ impl<'w, E, B: Bundle> Trigger<'w, E, B> {
5656
Ptr::from(&self.event)
5757
}
5858

59-
/// Returns the entity that triggered the observer, could be [`Entity::PLACEHOLDER`].
59+
/// Returns the [`Entity`] that triggered the observer, could be [`Entity::PLACEHOLDER`].
6060
pub fn entity(&self) -> Entity {
6161
self.trigger.entity
6262
}
6363

64+
/// Returns the [`Entity`] that observed the triggered event.
65+
/// This allows you to despawn the observer, ceasing observation.
66+
///
67+
/// # Examples
68+
///
69+
/// ```rust
70+
/// # use bevy_ecs::prelude::{Commands, Trigger};
71+
/// #
72+
/// # struct MyEvent {
73+
/// # done: bool,
74+
/// # }
75+
/// #
76+
/// /// Handle `MyEvent` and if it is done, stop observation.
77+
/// fn my_observer(trigger: Trigger<MyEvent>, mut commands: Commands) {
78+
/// if trigger.event().done {
79+
/// commands.entity(trigger.observer()).despawn();
80+
/// return;
81+
/// }
82+
///
83+
/// // ...
84+
/// }
85+
/// ```
86+
pub fn observer(&self) -> Entity {
87+
self.trigger.observer
88+
}
89+
6490
/// Enables or disables event propagation, allowing the same event to trigger observers on a chain of different entities.
6591
///
6692
/// The path an event will propagate along is specified by its associated [`Traversal`] component. By default, events

0 commit comments

Comments
 (0)