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

Events can be missed or double-counted by fixed time step systems #7691

Closed
alice-i-cecile opened this issue Feb 15, 2023 · 5 comments
Closed
Labels
A-ECS Entities, components, systems, and events A-Time Involves time keeping and reporting C-Bug An unexpected or incorrect behavior

Comments

@alice-i-cecile
Copy link
Member

Bevy version

Bevy 0.9 (and will still exist for Bevy 0.10)

What you did

  1. Send an event in an ordinary system.
  2. Read the event in a fixed timestep system.

What went wrong

Events are sometimes missed, and sometimes double counted.

Additional information

This occurs because events are only double-buffered, and a clean up system is run each frame via Events::::update_system.

If there are many passes of CoreSchedule::Update for each pass of CoreSchedule::FixedUpdate (due to short frame times), events will be missed. If the opposite is true, events will be double-counted.

#6183 is closely related, but not entirely the same, as the same problem occurs with the Input and Axis resources.

@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events A-Time Involves time keeping and reporting labels Feb 15, 2023
@alice-i-cecile alice-i-cecile added this to the 0.11 milestone Feb 15, 2023
@alice-i-cecile alice-i-cecile changed the title Events can be mixed by fixed time step systems Events can be missed or double-counted by fixed time step systems Feb 15, 2023
@alice-i-cecile
Copy link
Member Author

The cheatbook has a page on this problem, and some suggested workarounds: https://bevy-cheatbook.github.io/patterns/manual-event-clear.html

@jabuwu
Copy link
Contributor

jabuwu commented Feb 28, 2023

I've tried to make a small game using FixedUpdate in (unreleased) Bevy 0.10, and I briefly mentioned pitfalls in Discord, but I'll recap here.

The following problems need to be ironed out. I don't think any of them necessarily need to be "fixed" and they can all be worked around rather easily, which speaks to the strengths of Bevy, I think. But barring fixing them, we really need a page in the book or elsewhere dedicated to these problems, ways to work around them, and best practices with FixedUpdate in general. The cheatbook entry on this issue, as mentioned above, is very helpful.

#7691 (this issue)
#6183 (inputs can be missed in FixedUpdate)
#7835 (add a CoreSet to FixedUpdate)
#7836 (update GlobalTransform in FixedUpdate)

My vote is they all get addressed in a way that makes for the easiest developer experience, which is sometimes antithetical to performance. For example, updating GlobalTransform multiple times in a frame (and potentially many times due to the nature of FixedUpdate). So, I acknowledge my vote is probably controversial.

@starwolfy
Copy link

It would be great if FixedUpdate would also enjoy the missed event warning feature.

@paholg
Copy link

paholg commented Nov 5, 2023

It would be great if there were at least a workaround for built-in events. For example, I am trying to create a fully deterministic game with bevy and bevy_rapier.

However, bevy_rapier uses RemovedComponents for cleanup, and puts the necessary system in the main schedule, even when using FixedUpdate: https://github.com/dimforge/bevy_rapier/blob/0ea000b2938e6b55659023f1b14844cb51c10b69/src/plugin/plugin.rs#L218-L219

As far as I can tell, to make this deterministic, I would have to set a custom runner that calls World::clear_trackers in FixedUpdate instead of at the end of App::update. Given how much code is in winit_runner, I would like to avoid this.

@alice-i-cecile
Copy link
Member Author

This is now resolved: Events now integrate with the fixed update.

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-Time Involves time keeping and reporting C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests

4 participants