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 Observer
s 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
: TheEvent
derive defaults to using thisEntityTrigger
: TheEntityEvent
derive defaults to using thisPropagateEntityTrigger
: TheEntityEvent
derive 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::Trigger
must be constrained to the implementedTrigger
type, as part of the implementation. This prevents otherTrigger
implementations from directly deferring to your implementation, which is a very easy soundness misstep, as mostTrigger
implementations will invoke observers that are developed for their specificTrigger
type. Without this constraint, something likeGlobalTrigger
could be called for anyEvent
type, even one that expects a differentTrigger
type. This would result in an unsound cast ofGlobalTrigger
reference. 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
CachedObservers
observers
must come from theDeferredWorld
world
TriggerContext
must contain anEventKey
that matches theE
Event
typeobservers
must correspond to observers compatible with the event typeE
- 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 anevent
whoseEvent::Trigger
matches this trigger.