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 timer-based common run conditions #7864

Closed
alice-i-cecile opened this issue Mar 2, 2023 · 2 comments
Closed

Add timer-based common run conditions #7864

alice-i-cecile opened this issue Mar 2, 2023 · 2 comments
Labels
A-Time Involves time keeping and reporting C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Trivial Nice and easy! A great choice to get started with Bevy

Comments

@alice-i-cecile
Copy link
Member

What problem does this solve or what need does it fill?

Users often want to run a system (like logging, polling or factory ticks) on a regular timer. Currently, this involves a fair bit of type-level boilerplate to define a resource type, or clever use of either run conditions or Local system variables.

What solution would you like?

Create a run condition that stores a Timer in its Local, with a convenient method to generate such a run conditions.

Ideally this would look like:

app.add_system(my_system.run_if(on_timer(custom_duration))`

There should also be a on_fixed_timer variant, which uses elapsed FixedTime.

See the existing common run conditions in bevy_ecs and bevy_input for how to do so.

What alternative(s) have you considered?

We could add multiple fixed time steps again (which were previously used for this), but these are not equivalent and this previous pattern was incorrect. See #7637.

Additional context

Credit to @maniwani for the idea: I just made the ticket ;)

@alice-i-cecile alice-i-cecile added D-Trivial Nice and easy! A great choice to get started with Bevy C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Time Involves time keeping and reporting labels Mar 2, 2023
@jabuwu
Copy link
Contributor

jabuwu commented Mar 2, 2023

I'm working on it. I'm hoping to finally start my "bevy contributor" career and this seems easy enough. I wonder if on_timer_once conditions would be useful? I'm thinking no, they seem really niche.

Another potential API is on_timer(Timer) (instead of Duration), but its more wordy.

add_system(tick.run_if(on_timer(Timer::from_seconds(1., TimerMode::Repeating))))
// or
add_system(tick.run_if(on_timer(Duration::from_secs(1)))

I like the originally proposed idea, but throwing this out there.

@alice-i-cecile
Copy link
Member Author

Yeah, I think that the original suggestion is the most generally useful and comfortable of those. Users can make their own variants if they have unusual needs.

@bors bors bot closed this as completed in 98cf0d2 Mar 2, 2023
Shfty pushed a commit to shfty-rust/bevy that referenced this issue Mar 19, 2023
…`) (bevyengine#7866)

# Objective

Fixes bevyengine#7864

## Solution

Add the run conditions described in the issue. Also needed to add `bevy` as a dev dependency to `bevy_time` so the doctests can run.

---

## Changelog

- Add `on_timer` and `on_fixed_timer` run conditions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Time Involves time keeping and reporting C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Trivial Nice and easy! A great choice to get started with Bevy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants