bevy/release-content/release-notes/observer_overhaul.md
Alice Cecile b7d2cb8547
Provide access to the original target of entity-events in observers (#19663)
# Objective

Getting access to the original target of an entity-event is really
helpful when working with bubbled / propagated events.

`bevy_picking` special-cases this, but users have requested this for all
sorts of bubbled events.

The existing naming convention was also very confusing. Fixes
https://github.com/bevyengine/bevy/issues/17112, but also see #18982.

## Solution

1. Rename `ObserverTrigger::target` -> `current_target`.
1. Store `original_target: Option<Entity>` in `ObserverTrigger`.
1. Wire it up so this field gets set correctly.
1. Remove the `target` field on the `Pointer` events from
`bevy_picking`.

Closes https://github.com/bevyengine/bevy/pull/18710, which attempted
the same thing. Thanks @emfax!

## Testing

I've modified an existing test to check that the entities returned
during event bubbling / propagation are correct.

## Notes to reviewers

It's a little weird / sad that you can no longer access this infromation
via the buffered events for `Pointer`. That said, you already couldn't
access any bubbled target. We should probably remove the `BufferedEvent`
form of `Pointer` to reduce confusion and overhead, but I didn't want to
do so here.

Observer events can be trivially converted into buffered events (write
an observer with an EventWriter), and I suspect that that is the better
migration if you want the controllable timing or performance
characteristics of buffered events for your specific use case.

## Future work

It would be nice to not store this data at all (and not expose any
methods) if propagation was disabled. That involves more trait
shuffling, and I don't think we should do it here for reviewability.

---------

Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
2025-06-15 20:53:25 +00:00

1.4 KiB

title authors pull_requests
Observer Overhaul
@Jondolf
@alice-i-cecile
19596
19663

Rename Trigger to On

In past releases, the observer API looked like this:

app.add_observer(|trigger: Trigger<OnAdd, Player>| {
    info!("Added player {}", trigger.target());
});

In this example, the Trigger type contains information about the OnAdd event that was triggered for a Player.

Bevy 0.17 renames the Trigger type to On, and removes the On prefix from lifecycle events such as OnAdd and OnRemove:

app.add_observer(|trigger: On<Add, Player>| {
    info!("Added player {}", trigger.target());
});

This significantly improves readability and ergonomics, and is especially valuable in UI contexts where observers are very high-traffic APIs.

One concern that may come to mind is that Add can sometimes conflict with the core::ops::Add trait. However, in practice these scenarios should be rare, and when you do get conflicts, it should be straightforward to disambiguate by using ops::Add, for example.

Original targets

bevy_picking's Pointer events have always tracked the original target that an entity-event was targeting, allowing you to bubble events up your hierarchy to see if any of the parents care, then act on the entity that was actually picked in the first place.

This was handy! We've enabled this functionality for all entity-events: simply call On::original_target.