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):
GlobalTrigger: TheEventderive defaults to using thisEntityTrigger: TheEntityEventderive defaults to using thisPropagateEntityTrigger: TheEntityEventderive uses this when propagation is enabled.EntityComponentsTrigger: Used by Bevy’s component lifecycle events.
§Safety
Implementing this properly is advanced soundness territory! Implementers must abide by the following:
- The
E’Event::Triggermust be constrained to the implementedTriggertype, as part of the implementation. This prevents otherTriggerimplementations from directly deferring to your implementation, which is a very easy soundness misstep, as mostTriggerimplementations will invoke observers that are developed for their specificTriggertype. Without this constraint, something likeGlobalTriggercould be called for anyEventtype, even one that expects a differentTriggertype. This would result in an unsound cast ofGlobalTriggerreference. This is not expressed as an explicit type constraint,, as thefor<'a> Event::Trigger<'a>lifetime can mismatch explicit lifetimes in some impls.
Required Methods§
Sourceunsafe fn trigger(
&mut self,
world: DeferredWorld<'_>,
observers: &CachedObservers,
trigger_context: &TriggerContext,
event: &mut E,
)
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
CachedObserversobserversmust come from theDeferredWorldworld TriggerContextmust contain anEventKeythat matches theEEventtypeobserversmust correspond to observers compatible with the event typeE- Read and abide by the “Safety” section defined in the top-level
Triggerdocs. 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 aneventwhoseEvent::Triggermatches this trigger.