pub trait Event:
Sized
+ Send
+ Sync
+ 'static {
type Trigger<'a>: Trigger<Self>;
}
Expand description
An Event
is something that “happens” at a given moment.
To make an Event
“happen”, you “trigger” it on a World
using World::trigger
or via a Command
using Commands::trigger
. This causes any Observer
watching for that
Event
to run immediately, as part of the World::trigger
call.
First, we create an Event
type, typically by deriving the trait.
#[derive(Event)]
struct Speak {
message: String,
}
Then, we add an Observer
to watch for this event type:
world.add_observer(|speak: On<Speak>| {
println!("{}", speak.message);
});
Finally, we trigger the event by calling World::trigger
:
world.trigger(Speak {
message: "Hello!".to_string(),
});
§Triggers
Every Event
has an associated Trigger
implementation (set via Event::Trigger
), which defines which observers will run,
what data will be passed to them, and the order they will be run in. Unless you are an internals developer or you have very specific
needs, you don’t need to worry too much about Trigger
. When you derive Event
(or a more specific event trait like EntityEvent
),
a Trigger
will be provided for you.
The Event
derive defaults Event::Trigger
to GlobalTrigger
, which will run all observers that watch for the Event
.
§Entity Events
For events that “target” a specific Entity
, see EntityEvent
.
Required Associated Types§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.