#Running a function when the app is shutting down.

11 messages · Page 1 of 1 (latest)

velvet carbon
#

Hello, I am new to bevy and don't understand how I can run a function when the app is shutting down.

half sundial
#

Very basic way to do that would be something like this:

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(PhysicsPlugins::default())
        .add_plugins(PhysicsDebugPlugin::default())
        .add_plugins(WorldInspectorPlugin::default())
        .add_systems(Startup, startup)
        .add_systems(
            Update,
            spawn_player_ball.run_if(input_just_pressed(KeyCode::Space)),
        )
        .run();

    println!("This gets run once app is closed");
}
half sundial
velvet carbon
#

that is really useful. ty

velvet carbon
half sundial
velvet carbon
#

I tried both and it only got called with WindowClosing so I'll use that

stable vine
#

You should use AppExit if you want to run something when it's shutting down - a window closing is not guaranteed to do that, though it will if it's the only window and you're using the default behavior.

.add_systems(PostUpdate, do_stuff_on_exit.run_if(on_event::<AppExit>()).after(exit_on_primary_closed).after(exit_on_all_closed))```
#

I believe that snippet was originally for someone who needed to run something in PostUpdate specifically, this might work better for you rs .add_systems(Last, do_stuff_on_exit.run_if(on_event::<AppExit>()))