Skip to content
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

Add EventConsumer #2145

Closed
wants to merge 20 commits into from
Closed

Conversation

alice-i-cecile
Copy link
Member

Add an EventConsumer type, which consumes events rather than reading them. Good for simple automatic consumption, which is particularly likely to occur with the per-entity-events pattern found in #2116.

Moves impl blocks directly after types, organizes like-types together, presents shared functionality first
Also added handy drain_with_id method
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible labels May 11, 2021
@alice-i-cecile alice-i-cecile mentioned this pull request May 11, 2021
3 tasks
};

event_instances.map(|ei| {
trace!("Events::drain_with_id -> {}", ei.event_id);
Copy link
Contributor

@NathanSWard NathanSWard May 11, 2021

Choose a reason for hiding this comment

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

In the case we call drain() it may be a little confusing if our trace! message is "Events::drain_with_id", however I'm not sure if there is an obvious work around for this.... 🤔

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'm not super concerned about that; if you're using this trace functionality you're a relatively advanced user IMO.

Required to ensure Event readers continue functioning correctly
@alice-i-cecile alice-i-cecile marked this pull request as ready for review May 12, 2021 03:00
@alice-i-cecile
Copy link
Member Author

@NathanSWard this has all the functionality I want now and works correctly! Caught a serious existing bug in our Events::drain implementation where it wasn't resetting the metadata on the Events properly, which was revealed once I actually tried using it in an example.

Co-authored-by: Nathan Ward <[email protected]>
Comment on lines +20 to +21
// We can't use .add_event or our events will be cleaned up too soon
.init_resource::<Events<MyEvent>>()
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like this is a little nuisanced and could be easily missed by a user.
Is there a way to group an EventConsumer with add_event or warn if they are both added?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm. I don't think there's a sensible way to group it. The latter feels useful, but I'm not quite sure how we'd manage that.

Basically we'd need fairly global analysis to get that right; we need to see a) does any system use an EventConsumer and b) was add_event::T ever called.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe the answer is to rename add_event to be more explicit? I'm not sure what name you'd want there though :/

We can also ramp up the docs on both EventConsumer and add_event some more?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually: what if we added a cleanup field to add_event that explicitly controlled the behavior? I think that would help make things much more explicit and demistify the default behavior.

@alice-i-cecile
Copy link
Member Author

alice-i-cecile commented May 17, 2021

FYI, I'm still a bit iffy about the single consumer nature of this. It makes me very worried about maintainability of systems using this code. That said, this is much simpler than a proper multiconsumer solution like #1776 + bevyengine/rfcs#17 and builds on the existing architecture without any breaking changes.

@NathanSWard
Copy link
Contributor

FYI, I'm still a bit iffy about the single consumer nature of this. It makes me very worried about maintainability of systems using this code.

That's fair. Since this is a concern and we may not move forward with this, @alice-i-cecile is it alright if I rebase #2149 on main instead of this branch?

@alice-i-cecile
Copy link
Member Author

is it alright if I rebase #2149 on main instead of this branch?

Absolutely: that's an easy fix. FYI, the improvements I made to the drain methods are also strict improvements over the existing implementation, and solve a serious bug.

@alice-i-cecile alice-i-cecile marked this pull request as draft May 18, 2021 03:05
bors bot pushed a commit that referenced this pull request May 18, 2021
Taken from #2145

On draining, `Events` did not reset it's internal event count members.
@NathanSWard NathanSWard self-requested a review May 19, 2021 01:52
bors bot pushed a commit that referenced this pull request May 19, 2021
Taken from #2145

On draining and clearing, dangling `EventReaders` would not read into the correct event offset.
@NathanSWard
Copy link
Contributor

Do we have any further thoughts on this PR?
Some of the nuisances that EventConsumer bring to the table are not very user friendly.

@alice-i-cecile
Copy link
Member Author

I'm unhappy with both the current double buffering solution and the event consumer pattern here due to the likelihood of introducing unexpected and hard to mitigate bugs.

Seperate schedules may solve the fixed time step problem though, which would make me feel okay about double buffering again. Let's wait and see what comes of the pipelining work IMO.

@alice-i-cecile
Copy link
Member Author

Closing this out; it's not a great solution.

ostwilkens pushed a commit to ostwilkens/bevy that referenced this pull request Jul 27, 2021
Taken from bevyengine#2145

On draining and clearing, dangling `EventReaders` would not read into the correct event offset.
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-Feature A new feature, making something new possible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants