Skip to content

Commit

Permalink
Add observer to Trigger (#15066)
Browse files Browse the repository at this point in the history
# Objective

- Fixes  #15061

## Solution

- Added `observer` to `Trigger`, which returns the entity observing the
triggered event.

## Testing

- CI passed locally.
  • Loading branch information
bushrat011899 authored Sep 9, 2024
1 parent eaa87b8 commit 85e41dd
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion crates/bevy_ecs/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,37 @@ impl<'w, E, B: Bundle> Trigger<'w, E, B> {
Ptr::from(&self.event)
}

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

/// Returns the [`Entity`] that observed the triggered event.
/// This allows you to despawn the observer, ceasing observation.
///
/// # Examples
///
/// ```rust
/// # use bevy_ecs::prelude::{Commands, Trigger};
/// #
/// # struct MyEvent {
/// # done: bool,
/// # }
/// #
/// /// Handle `MyEvent` and if it is done, stop observation.
/// fn my_observer(trigger: Trigger<MyEvent>, mut commands: Commands) {
/// if trigger.event().done {
/// commands.entity(trigger.observer()).despawn();
/// return;
/// }
///
/// // ...
/// }
/// ```
pub fn observer(&self) -> Entity {
self.trigger.observer
}

/// Enables or disables event propagation, allowing the same event to trigger observers on a chain of different entities.
///
/// The path an event will propagate along is specified by its associated [`Traversal`] component. By default, events
Expand Down

0 comments on commit 85e41dd

Please sign in to comment.