-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Rename Trigger
to On
#19596
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
Rename Trigger
to On
#19596
Conversation
It looks like your PR has been selected for a highlight in the next release blog post, but you didn't provide a release note. Please review the instructions for writing release notes, then expand or revise the content in the release notes directory to showcase your changes. |
Marked as X-Blessed as per Cart's message on Discord earlier, but feel free to change :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bevy_eventlistener
is dead, long live bevy_eventlistener
. Everything old is new again. Time is a flat circle.
I do think this is a better design.
To ease migration, I've added the |
This is perfect, thank you. Can you do the same for |
Done! |
Objective
Currently, the observer API looks like this:
Future plans for observers also include "multi-event observers" with a trigger that looks like this (see Cart's example):
In scenarios like this, there is a lot of repetition of
On
. These are expected to be very high-traffic APIs especially in UI contexts, so ergonomics and readability are critical.By renaming
Trigger
toOn
, we can make these APIs read more cleanly and get rid of the repetition:Names like
On<Add<Pressed>>
emphasize the actual event listener nature more thanTrigger<OnAdd<Pressed>>
, and look cleaner. This also frees up theTrigger
name if we want to use it for the observer event type, splitting them out from buffered events (bikeshedding this is out of scope for this PR though).For prior art:
bevy_eventlistener
usedOn
for its event listener type. Though in our case, the observer is the event listener, andOn
is just a type containing information about the triggered event.Solution
Steal from
bevy_event_listener
by @aevyrie and useOn
.Trigger
toOn
OnAdd
toAdd
OnInsert
toInsert
OnReplace
toReplace
OnRemove
toRemove
OnDespawn
toDespawn
Discussion
Naming Conflicts??
Using a name like
Add
might initially feel like a very bad idea, since it risks conflict withcore::ops::Add
. However, I don't expect this to be a big problem in practice.Add
trait, especially in modules that would use the Bevy ECS.ops::Add
.Add
event is a struct while theAdd
trait is a trait (duh), so the compiler error should be very obvious.For the record, renaming
OnAdd
toAdd
, I got exactly zero errors or conflicts within Bevy itself. But this is of course not entirely representative of actual projects using Bevy.You might then wonder, why not use
Added
? This would conflict with theAdded
query filter, so it wouldn't work. Additionally, the current naming convention for observer events does not use past tense.Documentation
This does make documentation slightly more awkward when referring to
On
or its methods. Previous docs often referred toTrigger::target
or "sends aTrigger
" (which is... a bit strange anyway), which would now beOn::target
and "sends an observerEvent
".You can see the diff in this PR to see some of the effects. I think it should be fine though, we may just need to reword more documentation to read better.