mirror of
https://github.com/bevyengine/bevy
synced 2024-11-14 00:47:32 +00:00
de875fdc4c
# Objective Closes #13017. ## Solution - Make `AppExit` a enum with a `Success` and `Error` variant. - Make `App::run()` return a `AppExit` if it ever returns. - Make app runners return a `AppExit` to signal if they encountered a error. --- ## Changelog ### Added - [`App::should_exit`](https://example.org/) - [`AppExit`](https://docs.rs/bevy/latest/bevy/app/struct.AppExit.html) to the `bevy` and `bevy_app` preludes, ### Changed - [`AppExit`](https://docs.rs/bevy/latest/bevy/app/struct.AppExit.html) is now a enum with 2 variants (`Success` and `Error`). - The app's [runner function](https://docs.rs/bevy/latest/bevy/app/struct.App.html#method.set_runner) now has to return a `AppExit`. - [`App::run()`](https://docs.rs/bevy/latest/bevy/app/struct.App.html#method.run) now also returns the `AppExit` produced by the runner function. ## Migration Guide - Replace all usages of [`AppExit`](https://docs.rs/bevy/latest/bevy/app/struct.AppExit.html) with `AppExit::Success` or `AppExit::Failure`. - Any custom app runners now need to return a `AppExit`. We suggest you return a `AppExit::Error` if any `AppExit` raised was a Error. You can use the new [`App::should_exit`](https://example.org/) method. - If not exiting from `main` any other way. You should return the `AppExit` from `App::run()` so the app correctly returns a error code if anything fails e.g. ```rust fn main() -> AppExit { App::new() //Your setup here... .run() } ``` --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
44 lines
1.1 KiB
Rust
44 lines
1.1 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 {
|
|
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()
|
|
}
|