Skip to content

Provide access to the original target of entity-events in observers #19663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 15, 2025

Conversation

alice-i-cecile
Copy link
Member

@alice-i-cecile alice-i-cecile commented Jun 15, 2025

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 #17112, but also see #18982.

Solution

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

Closes #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.

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide M-Needs-Release-Note Work that should be called out in the blog due to impact X-Contentious There are nontrivial implications that should be thought through D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward A-Picking Pointing at and selecting objects of all sorts labels Jun 15, 2025
@viridia
Copy link
Contributor

viridia commented Jun 15, 2025

Naming: I suggested the named "listener" (to refer to the current target) and "emitter" (to refer to the original target). Note that "listener" used this way is consistent with the original bevy_eventlistener, while the term "emitter" is my own.

@alice-i-cecile
Copy link
Member Author

Naming: I suggested the named "listener" (to refer to the current target) and "emitter" (to refer to the original target). Note that "listener" used this way is consistent with the original bevy_eventlistener, while the term "emitter" is my own.

Not unreasonable, but I would really like to avoid doing that in this PR. I've opted for the least controversial names to avoid blocking this work on bikeshedding. #19263 is the place to discuss names.

Copy link
Contributor

@Jondolf Jondolf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly different from web conventions (target is the current target instead of the original one), but very reasonable. Happy to have the originally targeted entity available for more than just picking!

/// The entity that the entity-event targeted, if any.
///
/// Note that if event bubbling is enabled, this may not be the same as [`ObserverTrigger::original_target`].
pub current_target: Option<Entity>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Is there a reason to call this current_target even though On has target? I suppose it is clearer, just mildly inconsistent

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I decided for clarity over consistency here, but I can swap it.

Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
@alice-i-cecile
Copy link
Member Author

@emfax can I get your review here? :)

Copy link

@emfax emfax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it wise to remove target from Pointer.

current_target and original_target were actually my preferred names, but I didn't want to be too controversial and just introduced current_target.

Looks good to me.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 15, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jun 15, 2025
Merged via the queue into bevyengine:main with commit b7d2cb8 Jun 15, 2025
34 checks passed
@rendaoer
Copy link
Contributor

This is really great. I always struggle with whether to use pointer target or trigger target()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events A-Picking Pointing at and selecting objects of all sorts C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide M-Needs-Release-Note Work that should be called out in the blog due to impact S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Contentious There are nontrivial implications that should be thought through
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Target::target and Target::target() return different entities
5 participants