bevy/examples/ecs/system_stepping.rs

207 lines
7.4 KiB
Rust
Raw Permalink Normal View History

//! Demonstrate stepping through systems in order of execution.
System Stepping implemented as Resource (#8453) # Objective Add interactive system debugging capabilities to bevy, providing step/break/continue style capabilities to running system schedules. * Original implementation: #8063 - `ignore_stepping()` everywhere was too much complexity * Schedule-config & Resource discussion: #8168 - Decided on selective adding of Schedules & Resource-based control ## Solution Created `Stepping` Resource. This resource can be used to enable stepping on a per-schedule basis. Systems within schedules can be individually configured to: * AlwaysRun: Ignore any stepping state and run every frame * NeverRun: Never run while stepping is enabled - this allows for disabling of systems while debugging * Break: If we're running the full frame, stop before this system is run Stepping provides two modes of execution that reflect traditional debuggers: * Step-based: Only execute one system at a time * Continue/Break: Run all systems, but stop before running a system marked as Break ### Demo https://user-images.githubusercontent.com/857742/233630981-99f3bbda-9ca6-4cc4-a00f-171c4946dc47.mov Breakout has been modified to use Stepping. The game runs normally for a couple of seconds, then stepping is enabled and the game appears to pause. A list of Schedules & Systems appears with a cursor at the first System in the list. The demo then steps forward full frames using the spacebar until the ball is about to hit a brick. Then we step system by system as the ball impacts a brick, showing the cursor moving through the individual systems. Finally the demo switches back to frame stepping as the ball changes course. ### Limitations Due to architectural constraints in bevy, there are some cases systems stepping will not function as a user would expect. #### Event-driven systems Stepping does not support systems that are driven by `Event`s as events are flushed after 1-2 frames. Although game systems are not running while stepping, ignored systems are still running every frame, so events will be flushed. This presents to the user as stepping the event-driven system never executes the system. It does execute, but the events have already been flushed. This can be resolved by changing event handling to use a buffer for events, and only dropping an event once all readers have read it. The work-around to allow these systems to properly execute during stepping is to have them ignore stepping: `app.add_systems(event_driven_system.ignore_stepping())`. This was done in the breakout example to ensure sound played even while stepping. #### Conditional Systems When a system is stepped, it is given an opportunity to run. If the conditions of the system say it should not run, it will not. Similar to Event-driven systems, if a system is conditional, and that condition is only true for a very small time window, then stepping the system may not execute the system. This includes depending on any sort of external clock. This exhibits to the user as the system not always running when it is stepped. A solution to this limitation is to ensure any conditions are consistent while stepping is enabled. For example, all systems that modify any state the condition uses should also enable stepping. #### State-transition Systems Stepping is configured on the per-`Schedule` level, requiring the user to have a `ScheduleLabel`. To support state-transition systems, bevy generates needed schedules dynamically. Currently it’s very difficult (if not impossible, I haven’t verified) for the user to get the labels for these schedules. Without ready access to the dynamically generated schedules, and a resolution for the `Event` lifetime, **stepping of the state-transition systems is not supported** --- ## Changelog - `Schedule::run()` updated to consult `Stepping` Resource to determine which Systems to run each frame - Added `Schedule.label` as a `BoxedSystemLabel`, along with supporting `Schedule::set_label()` and `Schedule::label()` methods - `Stepping` needed to know which `Schedule` was running, and prior to this PR, `Schedule` didn't track its own label - Would have preferred to add `Schedule::with_label()` and remove `Schedule::new()`, but this PR touches enough already - Added calls to `Schedule.set_label()` to `App` and `World` as needed - Added `Stepping` resource - Added `Stepping::begin_frame()` system to `MainSchedulePlugin` - Run before `Main::run_main()` - Notifies any `Stepping` Resource a new render frame is starting ## Migration Guide - Add a call to `Schedule::set_label()` for any custom `Schedule` - This is only required if the `Schedule` will be stepped --------- Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2024-02-03 05:18:38 +00:00
use bevy::{ecs::schedule::Stepping, log::LogPlugin, prelude::*};
fn main() {
let mut app = App::new();
app
// to display log messages from Stepping resource
.add_plugins(LogPlugin::default())
.add_systems(
Update,
(
update_system_one,
// establish a dependency here to simplify descriptions below
update_system_two.after(update_system_one),
update_system_three.after(update_system_two),
update_system_four,
),
)
.add_systems(PreUpdate, pre_update_system);
// For the simplicity of this example, we directly modify the `Stepping`
// resource here and run the systems with `App::update()`. Each call to
// `App::update()` is the equivalent of a single frame render when using
// `App::run()`.
//
// In a real-world situation, the `Stepping` resource would be modified by
// a system based on input from the user. A full demonstration of this can
// be found in the breakout example.
println!(
r#"
Actions: call app.update()
Result: All systems run normally"#
);
app.update();
println!(
r#"
Actions: Add the Stepping resource then call app.update()
Result: All systems run normally. Stepping has no effect unless explicitly
configured for a Schedule, and Stepping has been enabled."#
);
app.insert_resource(Stepping::new());
app.update();
println!(
r#"
Actions: Add the Update Schedule to Stepping; enable Stepping; call
app.update()
Result: Only the systems in PreUpdate run. When Stepping is enabled,
systems in the configured schedules will not run unless:
* Stepping::step_frame() is called
* Stepping::continue_frame() is called
* System has been configured to always run"#
);
let mut stepping = app.world.resource_mut::<Stepping>();
stepping.add_schedule(Update).enable();
app.update();
println!(
r#"
Actions: call Stepping.step_frame(); call app.update()
Result: The PreUpdate systems run, and one Update system will run. In
Stepping, step means run the next system across all the schedules
that have been added to the Stepping resource."#
);
let mut stepping = app.world.resource_mut::<Stepping>();
stepping.step_frame();
app.update();
println!(
r#"
Actions: call app.update()
Result: Only the PreUpdate systems run. The previous call to
Stepping::step_frame() only applies for the next call to
app.update()/the next frame rendered.
"#
);
app.update();
println!(
r#"
Actions: call Stepping::continue_frame(); call app.update()
Result: PreUpdate system will run, and all remaining Update systems will
run. Stepping::continue_frame() tells stepping to run all systems
starting after the last run system until it hits the end of the
frame, or it encounters a system with a breakpoint set. In this
case, we previously performed a step, running one system in Update.
This continue will cause all remaining systems in Update to run."#
);
let mut stepping = app.world.resource_mut::<Stepping>();
stepping.continue_frame();
app.update();
println!(
r#"
Actions: call Stepping::step_frame() & app.update() four times in a row
Result: PreUpdate system runs every time we call app.update(), along with
one system from the Update schedule each time. This shows what
execution would look like to step through an entire frame of
systems."#
);
for _ in 0..4 {
let mut stepping = app.world.resource_mut::<Stepping>();
stepping.step_frame();
app.update();
}
println!(
r#"
Actions: Stepping::always_run(Update, update_system_two); step through all
systems
Result: PreUpdate system and update_system_two() will run every time we
call app.update(). We'll also only need to step three times to
execute all systems in the frame. Stepping::always_run() allows
us to granularly allow systems to run when stepping is enabled."#
);
let mut stepping = app.world.resource_mut::<Stepping>();
stepping.always_run(Update, update_system_two);
for _ in 0..3 {
let mut stepping = app.world.resource_mut::<Stepping>();
stepping.step_frame();
app.update();
}
println!(
r#"
Actions: Stepping::never_run(Update, update_system_two); continue through
all systems
Result: All systems except update_system_two() will execute.
Stepping::never_run() allows us to disable systems while Stepping
is enabled."#
);
let mut stepping = app.world.resource_mut::<Stepping>();
stepping.never_run(Update, update_system_two);
stepping.continue_frame();
app.update();
println!(
r#"
Actions: Stepping::set_breakpoint(Update, update_system_two); continue,
step, continue
Result: During the first continue, pre_update_system() and
update_system_one() will run. update_system_four() may also run
as it has no dependency on update_system_two() or
update_system_three(). Nether update_system_two() nor
update_system_three() will run in the first app.update() call as
they form a chained dependency on update_system_one() and run
in order of one, two, three. Stepping stops system execution in
the Update schedule when it encounters the breakpoint for
update_system_three().
During the step we run update_system_two() along with the
pre_update_system().
During the final continue pre_update_system() and
update_system_three() run."#
);
let mut stepping = app.world.resource_mut::<Stepping>();
stepping.set_breakpoint(Update, update_system_two);
stepping.continue_frame();
app.update();
let mut stepping = app.world.resource_mut::<Stepping>();
stepping.step_frame();
app.update();
let mut stepping = app.world.resource_mut::<Stepping>();
stepping.continue_frame();
app.update();
println!(
r#"
Actions: Stepping::clear_breakpoint(Update, update_system_two); continue
through all systems
Result: All systems will run"#
);
let mut stepping = app.world.resource_mut::<Stepping>();
stepping.clear_breakpoint(Update, update_system_two);
stepping.continue_frame();
app.update();
println!(
r#"
Actions: Stepping::disable(); app.update()
Result: All systems will run. With Stepping disabled, there's no need to
call Stepping::step_frame() or Stepping::continue_frame() to run
systems in the Update schedule."#
);
let mut stepping = app.world.resource_mut::<Stepping>();
stepping.disable();
app.update();
}
fn pre_update_system() {
println!("▶ pre_update_system");
}
fn update_system_one() {
println!("▶ update_system_one");
}
fn update_system_two() {
println!("▶ update_system_two");
}
fn update_system_three() {
println!("▶ update_system_three");
}
fn update_system_four() {
println!("▶ update_system_four");
}