Skip to content

Observer trigger refactor #19935

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 24 commits into from
Jul 4, 2025

Conversation

Zeophlite
Copy link
Contributor

@Zeophlite Zeophlite commented Jul 3, 2025

Objective

@alice-i-cecile alice-i-cecile added this to the 0.17 milestone Jul 3, 2025
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change S-Needs-Review Needs reviewer attention (from anyone!) to move forward M-Needs-Release-Note Work that should be called out in the blog due to impact labels Jul 3, 2025
@alice-i-cecile alice-i-cecile self-requested a review July 3, 2025 16:17
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

The TODO is fine in the release notes, but you should link this PR :) Once that's done this LGTM.

@hukasu
Copy link
Contributor

hukasu commented Jul 4, 2025

What is the motivation for this change? The hackMD linked on the previous PR is kinda big and the few search terms did not hit the part that mentions the motivation for this

@alice-i-cecile
Copy link
Member

What is the motivation for this change? The hackMD linked on the previous PR is kinda big and the few search terms did not hit the part that mentions the motivation for this

The usage of ComponentId is quite confusing: events are not components. By newtyping this, we can prevent stupid mistakes, avoid leaking internal details and make the code clearer for users and engine devs reading it.

@hukasu hukasu 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 Jul 4, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jul 4, 2025
Merged via the queue into bevyengine:main with commit 560429e Jul 4, 2025
34 checks passed
Copy link
Contributor

@SteveAlexander SteveAlexander left a comment

Choose a reason for hiding this comment

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

I get a sense of how this part of the code works, but I'm not sure I really understand it. Anyhow, I've left some review comments about some naming that, as a reader of the code, I found a bit confusing.

resource::Resource,
world::World,
};

#[doc(hidden)]
struct RegisteredEvent {
component_id: ComponentId,
event_key: EventKey,
// Required to flush the secondary buffer and drop events even if left unchanged.
previously_updated: bool,
// SAFETY: The component ID and the function must be used to fetch the Events<T> resource
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe the comment would be clearer if it said "The EventKey's component ID" ?

/// of the event passed into the observer system.
pub unsafe fn with_event(mut self, event: ComponentId) -> Self {
pub unsafe fn with_event(mut self, event: EventKey) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

In Observers::get_observers_mut, we have event_key: EventKey, and here we have event: EventKey. As a reader of the code, I'm wondering if they're different for a reason. Or are they just two ways of spelling the same thing?

Also, there are distinct structs Event and EventKey. In other parts of the PR, "the type of the event" was previously named event_type and changed to event_key. So maybe this arg should be event_key ?

@@ -182,7 +182,7 @@ pub struct ObserverTrigger {
/// The [`Entity`] of the observer handling the trigger.
pub observer: Entity,
/// The [`Event`] the trigger targeted.
pub event_type: ComponentId,
pub event_key: EventKey,
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand right, this isn't the Event as such, it's the EventKey that represents the type of the event. So the comment could be made more precise.

@@ -749,7 +749,7 @@ impl<'w> DeferredWorld<'w> {
#[inline]
pub(crate) unsafe fn trigger_observers(
&mut self,
event: ComponentId,
event: EventKey,
Copy link
Contributor

Choose a reason for hiding this comment

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

would it be clearer to name this event_key, as the type of the argument is EventKey not Event ?

@@ -773,7 +773,7 @@ impl<'w> DeferredWorld<'w> {
#[inline]
pub(crate) unsafe fn trigger_observers_with_data<E, T>(
&mut self,
event: ComponentId,
event: EventKey,
Copy link
Contributor

Choose a reason for hiding this comment

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

would it be clearer to name this event_key, as the type of the argument is EventKey not Event ?

@hukasu
Copy link
Contributor

hukasu commented Jul 9, 2025

@SteveAlexander the PR already went in, could you take your suggestions and open a new PR?

@SteveAlexander
Copy link
Contributor

Thanks @hukasu, I responded to a request to review on Discord, and didn't check the timestamp :-)

Yes, I'll open a new PR.

@SteveAlexander
Copy link
Contributor

@hukasu opened a new PR here #20060

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 C-Code-Quality A section of code that is hard to understand or change 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants