Struct On

Source
pub struct On<'w, 't, 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 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: Event, B: Bundle> On<'w, 't, E, B>

Source

pub fn new( event: &'w mut E, observer: Entity, trigger: &'w mut E::Trigger<'t>, trigger_context: &'w TriggerContext, ) -> Self

Creates a new instance of On for the given triggered event.

Source

pub fn event_key(&self) -> EventKey

Returns the event type of this On instance.

Source

pub fn event(&self) -> &E

Returns a reference to the triggered event.

Source

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

Returns a mutable reference to the triggered event.

Source

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

Returns a pointer to the triggered event.

Source

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

Returns the Trigger context for this event.

Source

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

Returns the mutable Trigger context for this event.

Source

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

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: EntityEvent, B: Bundle> On<'w, 't, E, B>

Source

pub 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.

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: EntityEvent + for<'a> Event<Trigger<'a> = PropagateEntityTrigger<AUTO_PROPAGATE, E, T>>, B: Bundle, T: Traversal<E>> On<'w, 't, E, B>

Source

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.

Source

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.

Source

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<'w, 't, E: for<'a> Event<Trigger<'a>: Debug> + Debug, B: Bundle> Debug for On<'w, 't, E, B>

Source§

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

Formats the value using the given formatter. Read more
Source§

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

Source§

type Target = E

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

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

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

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

Used for ObserverSystems.

Source§

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

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

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

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

fn wrap(this: Self::Inner<'_>) -> Self::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 = ()> !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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

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

Converts 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)

Converts &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)

Converts &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
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.

Source§

impl<T> IntoResult<T> for T

Source§

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

Converts this type into the system output type.
Source§

impl<A> Is for A
where A: Any,

Source§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. Read more
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.
Source§

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