Path: blob/main/release-content/release-notes/event_split.md
6592 views
---
---
In past releases, all event types were defined by simply deriving the Event
trait:
You could then use the various event handling tools in Bevy to send and listen to the event. The common options include:
Use
trigger
to trigger the event and react to it with a globalObserver
Use
trigger_targets
to trigger the event with specific entity target(s) and react to it with an entityObserver
or globalObserver
Use
EventWriter::write
to write the event to an event buffer andEventReader::read
to read it at a later time
The first two are observer APIs, while the third is a fully separate "buffered" API for pull-based event handling. All three patterns are fundamentally different in both the interface and usage. Despite the same event type being used everywhere, APIs are typically built to support only one of them.
This has led to a lot of confusion and frustration for users. A common footgun was using a "buffered event" with an observer, or an observer event with EventReader
, leaving the user wondering why the event is not being detected.
Bevy 0.17 aims to solve this ambiguity by splitting the event traits into Event
, EntityEvent
, and BufferedEvent
.
Event
: A shared trait for observer events.EntityEvent
: AnEvent
that additionally supports targeting specific entities and propagating the event from one entity to another.BufferedEvent
: An event that supports usage withEventReader
andEventWriter
for pull-based event handling.
Using Events
A basic Event
can be defined like before, by deriving the Event
trait.
You can then trigger
the event, and use a global observer for reacting to it.
To allow an event to be targeted at entities and even propagated further, you can instead derive EntityEvent
. It supports optionally specifying some options for propagation using the event
attribute:
Every EntityEvent
is also an Event
, so you can still use trigger
to trigger them globally. However, entity events also support targeted observer APIs such as trigger_targets
and observe
:
To allow an event to be used with the buffered API, you can instead derive BufferedEvent
:
The event can then be used with EventReader
/EventWriter
:
In summary:
Need a basic event you can trigger and observe? Derive
Event
!Need the observer event to be targeted at an entity? Derive
EntityEvent
!Need the event to be buffered and support the
EventReader
/EventWriter
API? DeriveBufferedEvent
!