Trigger

Trait Trigger 

Source
pub unsafe trait Trigger<E>
where E: Event,
{ // Required method unsafe fn trigger( &mut self, world: DeferredWorld<'_>, observers: &CachedObservers, trigger_context: &TriggerContext, event: &mut E, ); }
Expand description

Trigger determines how an Event is triggered when World::trigger is called. This decides which Observers will run, what data gets passed to them, and the order they will be executed in.

Implementing Trigger is “advanced-level” territory, and is generally unnecessary unless you are developing highly specialized Event trigger logic.

Bevy comes with a number of built-in Trigger implementations (see their documentation for more info):

§Safety

Implementing this properly is advanced soundness territory! Implementers must abide by the following:

  • The EEvent::Trigger must be constrained to the implemented Trigger type, as part of the implementation. This prevents other Trigger implementations from directly deferring to your implementation, which is a very easy soundness misstep, as most Trigger implementations will invoke observers that are developed for their specific Trigger type. Without this constraint, something like GlobalTrigger could be called for any Event type, even one that expects a different Trigger type. This would result in an unsound cast of GlobalTrigger reference. This is not expressed as an explicit type constraint,, as the for<'a> Event::Trigger<'a> lifetime can mismatch explicit lifetimes in some impls.

Required Methods§

Source

unsafe fn trigger( &mut self, world: DeferredWorld<'_>, observers: &CachedObservers, trigger_context: &TriggerContext, event: &mut E, )

Trigger the given event, running every Observer that matches the event, as defined by this Trigger and the state stored on self.

§Safety
  • The CachedObservers observers must come from the DeferredWorld world
  • TriggerContext must contain an EventKey that matches the E Event type
  • observers must correspond to observers compatible with the event type E
  • Read and abide by the “Safety” section defined in the top-level Trigger docs. Calling this function is unintuitively risky. Do not use it directly unless you know what you are doing. Importantly, this should only be called for an event whose Event::Trigger matches this trigger.

Implementors§

Source§

impl<'a, E> Trigger<E> for EntityComponentsTrigger<'a>
where E: EntityEvent<Trigger<'a> = EntityComponentsTrigger<'a>> + Event,

Source§

impl<E> Trigger<E> for EntityTrigger
where E: EntityEvent<Trigger<'a> = EntityTrigger> + for<'a> Event,

Source§

impl<E> Trigger<E> for GlobalTrigger
where E: for<'a> Event<Trigger<'a> = GlobalTrigger>,

Source§

impl<const AUTO_PROPAGATE: bool, E, T> Trigger<E> for PropagateEntityTrigger<AUTO_PROPAGATE, E, T>
where E: EntityEvent<Trigger<'a> = PropagateEntityTrigger<AUTO_PROPAGATE, E, T>> + for<'a> Event, T: Traversal<E>,