bevyengine / bevyengine/bevy

Allow observers to run on a timer being finished

Open
#18,894 4 comments 0 reactions 0 assignees View on GitHub
A-Time C-Feature
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

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

As noted by #15129 the current implementation for doing stuff on timers is very boilerplate heavy.

## What solution would you like?

Some way to run observers when a timer is finished.

```rust
fn spawn_flashy_thing(mut commands: Commands) {
commands.spawn(Camera2d);
commands
.spawn((
BackgroundColor(Color::from(css::RED)),
ObserveredTimer(Timer::new(Duration::from_secs(1), TimerMode::Repeating)),
))
.observe(flash_flashy_thing);
}

const COLORS: [Color; 7] = [
...
];

fn flash_flashy_thing(
flashy_thing: Trigger,
mut colors: Query<&mut BackgroundColor>,
mut color_position: Local,
) -> Result<(), BevyError> {
*color_position += 1;

new_color = COLORS[*color_position % 7];

let mut color = colors.get_mut(flashy_thing.target())?;

color.0 = new_color;

Ok(())
}
```

Obviously this isn't perfect and there's probably some improvements to be made. For one it doesn't allow for multiple different timers on one entity which would be an issue for some.

## What alternative(s) have you considered?

The old boilerplate heavy way of doing it.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.