pub trait Event:
Send
+ Sync
+ 'static {
type Traversal: Traversal<Self>;
const AUTO_PROPAGATE: bool = false;
// Provided methods
fn register_component_id(world: &mut World) -> ComponentId { ... }
fn component_id(world: &World) -> Option<ComponentId> { ... }
}
Expand description
Something that “happens” and might be read / observed by app logic.
Events can be stored in an Events<E>
resource
You can conveniently access events using the EventReader
and EventWriter
system parameter.
Events can also be “triggered” on a World
, which will then cause any Observer
of that trigger to run.
Events must be thread-safe.
§Derive
This trait can be derived.
Adding auto_propagate
sets Self::AUTO_PROPAGATE
to true.
Adding traversal = "X"
sets Self::Traversal
to be of type “X”.
use bevy_ecs::prelude::*;
#[derive(Event)]
#[event(auto_propagate)]
struct MyEvent;
Provided Associated Constants§
Sourceconst AUTO_PROPAGATE: bool = false
const AUTO_PROPAGATE: bool = false
When true, this event will always attempt to propagate when triggered, without requiring a call
to Trigger::propagate
.
Required Associated Types§
Sourcetype Traversal: Traversal<Self>
type Traversal: Traversal<Self>
The component that describes which Entity to propagate this event to next, when propagation is enabled.
Provided Methods§
Sourcefn register_component_id(world: &mut World) -> ComponentId
fn register_component_id(world: &mut World) -> ComponentId
Generates the ComponentId
for this event type.
If this type has already been registered,
this will return the existing ComponentId
.
This is used by various dynamically typed observer APIs,
such as World::trigger_targets_dynamic
.
§Warning
This method should not be overridden by implementors,
and should always correspond to the implementation of component_id
.
Sourcefn component_id(world: &World) -> Option<ComponentId>
fn component_id(world: &World) -> Option<ComponentId>
Fetches the ComponentId
for this event type,
if it has already been generated.
This is used by various dynamically typed observer APIs,
such as World::trigger_targets_dynamic
.
§Warning
This method should not be overridden by implementors,
and should always correspond to the implementation of register_component_id
.
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.