bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Time trackers

Open Brezak opened this issue 1 year ago • 0 comments

Objective

Users currently need to update timers and stopwatches themselves. This introduces 2 main issues:

  • Users may forget to update their timers.
  • Timers read from both Update and FixedUpdate need special considerations when updating them

Solution

  • Introduce 2 new component versions of timers and stopwatches.
    • Updating* time trackers track time based on their generic argument (e.g. UpdatingTimer<Virtual> will track Time<Virtual>).
    • Mixed* time trackers track both virtual and fixed time and correctly report time based on which schedule they're read from.
  • Introduce a TimeTracker trait defining how to update a time tracker. The intent is to eventually provide a derive macro.
  • Introduce a Context trait to distinguish Time<()> from all the time types. This is used to constrain what time type the updating time trackers track.
  • Introduce a TimesWithContext to define what times a TimeTracker tracks.
  • Introduce a TimeTrackingAppExtension trait that helps with registering user defined time trackers into the app.
The `TimeTracker` trait
pub trait TimeTracker {
    type Time: TimesWithContext;

    fn update(
        &mut self,
        time: &<<Self::Time as TimesWithContext>::AsSystemParam<'_> as SystemParam>::Item<'_, '_>,
    );

    fn enter_fixed_update(&mut self) {}
    fn exit_fixed_update(&mut self) {}
}

Closes #13420.

Future work

  • A TimeTracker derive macro.
  • Automatic cleanup of finished time trackers.

Testing

  • Did you test these changes? If so, how?
  • Are there any parts that need more testing?
  • How can other people (reviewers) test your changes? Is there anything specific they need to know?
  • If relevant, what platforms did you test these changes on, and are there any important ones you can't test?

Showcase

This section is optional. If this PR does not include a visual change or does not add a new feature, you can delete this section.

  • Help others understand the result of this PR by showcasing your awesome work!
  • If this PR adds a new feature or public API, consider adding a brief pseudo-code snippet of it in action
  • If this PR includes a visual change, consider adding a screenshot, GIF, or video
    • If you want, you could even include a before/after comparison!
  • If the Migration Guide adequately covers the changes, you can delete this section

While a showcase should aim to be brief and digestible, you can use a toggleable section to save space on longer showcases:

Click to view showcase
println!("My super cool code.");

Brezak avatar Jul 11 '24 12:07 Brezak