mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
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.
This commit is contained in:
parent
eaa87b8c34
commit
85e41ddace
1 changed files with 27 additions and 1 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue