bevy/examples/app/custom_loop.rs
Philpax 99b4fb68cc
Fix custom_loop example to include plugin finalization (#13215)
# Objective

The `custom_loop` example didn't replicate the `app.finish` /
`app.cleanup` calls from the default runner; I discovered this when
trying to troubleshoot why my application with a custom loop wasn't
calling its plugin finalizers, and realised that the upstream example
that I'd referenced didn't have the relevant calls.

## Solution

Added the missing calls, replicating what the default runner does:

d390420093/crates/bevy_app/src/app.rs (L895-L896)

## Testing

I've confirmed that adding these two calls to my application fixed the
issue I was encountering. I haven't tested it within the example itself
as it's relatively straightforward and I didn't want to pollute the
example with a plugin using a finalizer.
2024-05-03 20:12:27 +00:00

49 lines
1.3 KiB
Rust

//! This example demonstrates you can create a custom runner (to update an app manually). It reads
//! lines from stdin and prints them from within the ecs.
use bevy::{app::AppExit, prelude::*};
use std::io;
#[derive(Resource)]
struct Input(String);
fn my_runner(mut app: App) -> AppExit {
// Finalize plugin building, including running any necessary clean-up.
// This is normally completed by the default runner.
app.finish();
app.cleanup();
println!("Type stuff into the console");
for line in io::stdin().lines() {
{
let mut input = app.world_mut().resource_mut::<Input>();
input.0 = line.unwrap();
}
app.update();
if let Some(exit) = app.should_exit() {
return exit;
}
}
AppExit::Success
}
fn print_system(input: Res<Input>) {
println!("You typed: {}", input.0);
}
fn exit_system(input: Res<Input>, mut exit_event: EventWriter<AppExit>) {
if input.0 == "exit" {
exit_event.send(AppExit::Success);
}
}
// AppExit implements `Termination` so we can return it from main.
fn main() -> AppExit {
App::new()
.insert_resource(Input(String::new()))
.set_runner(my_runner)
.add_systems(Update, (print_system, exit_system))
.run()
}