bevy/examples/ecs/fixed_timestep.rs
JoJoJet b8263b55fb Support system.in_schedule() and system.on_startup() (#7790)
# Objective

Support the following syntax for adding systems:

```rust
App::new()
    .add_system(setup.on_startup())
    .add_systems((
        show_menu.in_schedule(OnEnter(GameState::Paused)),
        menu_ssytem.in_set(OnUpdate(GameState::Paused)),
        hide_menu.in_schedule(OnExit(GameState::Paused)),
    ))
```

## Solution

Add the traits `IntoSystemAppConfig{s}`, which provide the extension methods necessary for configuring which schedule a system belongs to. These extension methods return `IntoSystemAppConfig{s}`, which `App::add_system{s}` uses to choose which schedule to add systems to.

---

## Changelog

+ Added the extension methods `in_schedule(label)` and  `on_startup()` for configuring the schedule a system belongs to.

## Future Work

* Replace all uses of `add_startup_system` in the engine.
* Deprecate this method
2023-02-24 18:33:55 +00:00

38 lines
1.2 KiB
Rust

//! Shows how to create systems that run every fixed timestep, rather than every tick.
use bevy::prelude::*;
const FIXED_TIMESTEP: f32 = 0.5;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
// this system will run once every update (it should match your screen's refresh rate)
.add_system(frame_update)
// add our system to the fixed timestep schedule
.add_system(fixed_update.in_schedule(CoreSchedule::FixedUpdate))
// configure our fixed timestep schedule to run twice a second
.insert_resource(FixedTime::new_from_secs(FIXED_TIMESTEP))
.run();
}
fn frame_update(mut last_time: Local<f32>, time: Res<Time>) {
info!(
"time since last frame_update: {}",
time.raw_elapsed_seconds() - *last_time
);
*last_time = time.raw_elapsed_seconds();
}
fn fixed_update(mut last_time: Local<f32>, time: Res<Time>, fixed_time: Res<FixedTime>) {
info!(
"time since last fixed_update: {}\n",
time.raw_elapsed_seconds() - *last_time
);
info!("fixed timestep: {}\n", FIXED_TIMESTEP);
info!(
"time accrued toward next fixed_update: {}\n",
fixed_time.accumulated().as_secs_f32()
);
*last_time = time.raw_elapsed_seconds();
}