Skip to main content

On

Struct On 

pub struct On<'w, 't, E, B = ()>
where E: Event, B: Bundle,
{ /* 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 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§

§

impl<'w, 't, E, B> On<'w, 't, E, B>
where E: Event, B: Bundle,

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.

pub fn event_key(&self) -> EventKey

Returns the event type of this On instance.

pub fn event(&self) -> &E

Returns a reference to the triggered event.

pub fn event_mut(&mut self) -> &mut E

Returns a mutable reference to the triggered event.

pub fn event_ptr(&self) -> Ptr<'_>

Returns a pointer to the triggered event.

pub fn trigger(&self) -> &<E as Event>::Trigger<'t>

Returns the Trigger context for this event.

pub fn trigger_mut(&mut self) -> &mut <E as Event>::Trigger<'t>

Returns the mutable Trigger context for this event.

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 });  

pub fn caller(&self) -> MaybeLocation

Returns the source code location that triggered this observer, if the track_location cargo feature is enabled.

§

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>,

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.

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)]. See EntityEvent for details.
  • Either call propagate(true) in the first observer or in the EntityEvent 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.

pub fn get_propagate(&self) -> bool

Returns the value of the flag that controls event propagation. See propagate for more information.

Trait Implementations§

§

impl<'w, 't, E, B> Debug for On<'w, 't, E, B>
where E: for<'a> Event + Debug, <E as Event>::Trigger<'a>: for<'a> Debug, B: Bundle,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<'w, 't, E, B> Deref for On<'w, 't, E, B>
where E: Event, B: Bundle,

§

type Target = E

The resulting type after dereferencing.
§

fn deref(&self) -> &<On<'w, 't, E, B> as Deref>::Target

Dereferences the value.
§

impl<'w, 't, E, B> DerefMut for On<'w, 't, E, B>
where E: Event, B: Bundle,

§

fn deref_mut(&mut self) -> &mut <On<'w, 't, E, B> as Deref>::Target

Mutably dereferences the value.
§

impl<E, B> SystemInput for On<'_, '_, E, B>
where E: Event, B: Bundle,

Used for ObserverSystems.

§

type Param<'i> = On<'i, 'i, E, B>

The wrapper input type that is defined as the first argument to FunctionSystems.
§

type Inner<'i> = On<'i, 'i, E, B>

The inner input type that is passed to functions that run systems, such as System::run.
§

fn wrap( this: <On<'_, '_, E, B> as SystemInput>::Inner<'_>, ) -> <On<'_, '_, E, B> as 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>
where <E as Event>::Trigger<'t>: Send,

§

impl<'w, 't, E, B> Sync for On<'w, 't, E, B>
where <E as Event>::Trigger<'t>: Sync,

§

impl<'w, 't, E, B> Unpin for On<'w, 't, E, B>
where B: Unpin,

§

impl<'w, 't, E, B> UnsafeUnpin for On<'w, 't, E, B>

§

impl<'w, 't, E, B = ()> !UnwindSafe for On<'w, 't, E, B>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<In> FromInput<In> for In
where In: SystemInput,

§

fn from_inner<'i>( inner: <In as SystemInput>::Inner<'i>, ) -> <In as SystemInput>::Inner<'i>

Converts the system input’s inner representation into this type’s inner representation.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> IntoResult<T> for T

§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ConditionalSend for T
where T: Send,