pub struct On<'w, 't, E, B = ()>{ /* private fields */ }
Expand description
A system parameter used by an observer to process events. See Observer
and Event
for examples.
On
contains the triggered Event
data for a given run of an Observer
. It also provides access to the
Trigger
, which for things like EntityEvent
with a PropagateEntityTrigger
,
includes control over event propagation.
The generic B: Bundle
is used to further specialize the events that this observer is interested in.
The entity involved does not have to have these components, but the observer will only be
triggered if the event matches the components in B
.
This is used to to avoid providing a generic argument in your event, as is done for Add
and the other lifecycle events.
Providing multiple components in this bundle will cause this event to be triggered by any matching component in the bundle, rather than requiring all of them to be present.
Implementations§
Source§impl<'w, 't, E, B> On<'w, 't, E, B>
impl<'w, 't, E, B> On<'w, 't, E, B>
Sourcepub fn new(
event: &'w mut E,
observer: Entity,
trigger: &'w mut <E as Event>::Trigger<'t>,
trigger_context: &'w TriggerContext,
) -> On<'w, 't, E, B>
pub fn new( event: &'w mut E, observer: Entity, trigger: &'w mut <E as Event>::Trigger<'t>, trigger_context: &'w TriggerContext, ) -> On<'w, 't, E, B>
Creates a new instance of On
for the given triggered event.
Sourcepub fn trigger(&self) -> &<E as Event>::Trigger<'t>
pub fn trigger(&self) -> &<E as Event>::Trigger<'t>
Returns the Trigger
context for this event.
Sourcepub fn trigger_mut(&mut self) -> &mut <E as Event>::Trigger<'t>
pub fn trigger_mut(&mut self) -> &mut <E as Event>::Trigger<'t>
Returns the mutable Trigger
context for this event.
Sourcepub fn observer(&self) -> Entity
pub fn observer(&self) -> Entity
Returns the Entity
of the Observer
of the triggered event.
This allows you to despawn the observer, ceasing observation.
§Examples
#[derive(EntityEvent)]
struct AssertEvent {
entity: Entity,
}
fn assert_observer(event: On<AssertEvent>) {
assert_eq!(event.observer(), event.entity);
}
let mut world = World::new();
let entity = world.spawn(Observer::new(assert_observer)).id();
world.trigger(AssertEvent { entity });
Sourcepub fn caller(&self) -> MaybeLocation
pub fn caller(&self) -> MaybeLocation
Returns the source code location that triggered this observer, if the track_location
cargo feature is enabled.
Source§impl<'w, 't, E, B> On<'w, 't, E, B>where
E: EntityEvent,
B: Bundle,
impl<'w, 't, E, B> On<'w, 't, E, B>where
E: EntityEvent,
B: Bundle,
Sourcepub fn target(&self) -> Entity
👎Deprecated since 0.17.0: Call On::event() to access the event, then read the target entity from the event directly.
pub fn target(&self) -> Entity
A deprecated way to retrieve the entity that this EntityEvent
targeted at.
Access the event via On::event
, then read the entity that the event was targeting.
Prefer using the field name directly for clarity,
but if you are working in a generic context, you can use EntityEvent::event_target
.
Source§impl<'w, 't, const AUTO_PROPAGATE: bool, E, B, T> On<'w, 't, E, B>where
E: EntityEvent<Trigger<'a> = PropagateEntityTrigger<AUTO_PROPAGATE, E, T>> + for<'a> Event,
B: Bundle,
T: Traversal<E>,
impl<'w, 't, const AUTO_PROPAGATE: bool, E, B, T> On<'w, 't, E, B>where
E: EntityEvent<Trigger<'a> = PropagateEntityTrigger<AUTO_PROPAGATE, E, T>> + for<'a> Event,
B: Bundle,
T: Traversal<E>,
Sourcepub fn original_event_target(&self) -> Entity
pub fn original_event_target(&self) -> Entity
Returns the original Entity
that this EntityEvent
targeted via EntityEvent::event_target
when it was first triggered,
prior to any propagation logic.
Sourcepub fn propagate(&mut self, should_propagate: bool)
pub fn propagate(&mut self, should_propagate: bool)
Enables or disables event propagation, allowing the same event to trigger observers on a chain of different entities.
The path an EntityEvent
will propagate along is specified by the Traversal
component defined in PropagateEntityTrigger
.
EntityEvent
does not propagate by default. To enable propagation, you must:
- Enable propagation in
EntityEvent
using#[entity_event(propagate)]
. SeeEntityEvent
for details. - Either call
propagate(true)
in the first observer or in theEntityEvent
derive add#[entity_event(auto_propagate)]
.
You can prevent an event from propagating further using propagate(false)
. This will prevent the event from triggering on the next
Entity
in the Traversal
, but note that all remaining observers for the current entity will still run.
Sourcepub fn get_propagate(&self) -> bool
pub fn get_propagate(&self) -> bool
Returns the value of the flag that controls event propagation. See propagate
for more information.
Trait Implementations§
Source§impl<E, B> SystemInput for On<'_, '_, E, B>
Used for ObserverSystem
s.
impl<E, B> SystemInput for On<'_, '_, E, B>
Used for ObserverSystem
s.
Source§type Param<'i> = On<'i, 'i, E, B>
type Param<'i> = On<'i, 'i, E, B>
FunctionSystem
s.Source§type Inner<'i> = On<'i, 'i, E, B>
type Inner<'i> = On<'i, 'i, E, B>
System::run
.Source§fn wrap(
this: <On<'_, '_, E, B> as SystemInput>::Inner<'_>,
) -> <On<'_, '_, E, B> as SystemInput>::Param<'_>
fn wrap( this: <On<'_, '_, E, B> as SystemInput>::Inner<'_>, ) -> <On<'_, '_, E, B> as SystemInput>::Param<'_>
SystemInput::Inner
into a SystemInput::Param
.Auto Trait Implementations§
impl<'w, 't, E, B> Freeze for On<'w, 't, E, B>
impl<'w, 't, E, B> RefUnwindSafe for On<'w, 't, E, B>
impl<'w, 't, E, B> Send for On<'w, 't, E, B>
impl<'w, 't, E, B> Sync for On<'w, 't, E, B>
impl<'w, 't, E, B> Unpin for On<'w, 't, E, B>where
B: Unpin,
impl<'w, 't, E, B = ()> !UnwindSafe for On<'w, 't, E, B>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
, which can then be
downcast
into Box<dyn ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
, which can then be further
downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more