Tainted \\ Coders

Run a plugin conditionally

Last updated:

Plugins are statically configured when your App is built so there is no real way to add or remove them from your app during runtime.

You can however use run conditions or some other logic to have them run when your game is in a certain state.

#[derive(Debug, Clone, Eq, PartialEq, Hash, Default, States)]
enum GameState {
    #[default]
    MainMenu,
    Game,
}

fn system_a() {
    println!("System A");
}

fn system_b() {
    println!("System B");
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(
            Update,
            system_a
                .run_if(in_state(GameState::MainMenu))
        )
        .add_systems(
            Update,
            system_b
                .run_if(in_state(GameState::Game))
        )
        .run();
}