Skip to content

Commit 10a3637

Browse files
committed
Add AppExit to examples
1 parent 70f2d97 commit 10a3637

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

crates/bevy_app/src/app.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ impl App {
187187
/// loop {
188188
/// println!("In main loop");
189189
/// app.update();
190+
/// if let Some(exit) = app.should_exit() {
191+
/// return exit;
192+
/// }
190193
/// }
191194
/// }
192195
///

crates/bevy_app/src/schedule_runner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl Plugin for ScheduleRunnerPlugin {
149149
let base_tick_closure = moved_tick_closure.clone();
150150

151151
let tick_app = move || {
152-
let mut app = Rc::get_mut(&mut app).unwrap();
152+
let app = Rc::get_mut(&mut app).unwrap();
153153
let delay = tick(app, wait);
154154
match delay {
155155
Ok(delay) => set_timeout(

examples/app/custom_loop.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ fn my_runner(mut app: App) -> AppExit {
1515
input.0 = line.unwrap();
1616
}
1717
app.update();
18+
19+
if let Some(exit) = app.should_exit() {
20+
return exit;
21+
}
1822
}
1923

2024
AppExit::Success
@@ -24,10 +28,17 @@ fn print_system(input: Res<Input>) {
2428
println!("You typed: {}", input.0);
2529
}
2630

27-
fn main() {
31+
fn exit_system(input: Res<Input>, mut exit_event: EventWriter<AppExit>) {
32+
if input.0 == "exit" {
33+
exit_event.send(AppExit::Success);
34+
}
35+
}
36+
37+
// AppExit implements `Termination` so we can return it from main.
38+
fn main() -> AppExit {
2839
App::new()
2940
.insert_resource(Input(String::new()))
3041
.set_runner(my_runner)
31-
.add_systems(Update, print_system)
32-
.run();
42+
.add_systems(Update, (print_system, exit_system))
43+
.run()
3344
}

0 commit comments

Comments
 (0)