Skip to content

Latest commit

 

History

History
75 lines (56 loc) · 2.43 KB

turning_on_off_a_system.md

File metadata and controls

75 lines (56 loc) · 2.43 KB

Turning On/Off A System

In addition to schedule labels, such as Startup and Update, we can have more control on Systems. There are functions/methods located at bevy::ecs::schedule::common_conditions as well as IntoSystemConfigs that help us to control systems.

In the following example, we add two Systems to the moment Startup. However, only one of the systems will actually run. The other system will not run.

We use the function run_if to set a condition specifying when the system can be run.

add_systems(Startup, print_a.run_if(always_true))

The function run_if takes a system, always_true here, which returns a bool indicating whether the system should be run. The system always_true is a simple system created by us.

fn always_true() -> bool {
    return true;
}

When a system is acted as a condition, it can be preceded with the function not, to inverse its output.

add_systems(Startup, print_b.run_if(not(always_true)))

The full code is as follows:

use bevy::{
    app::{App, Startup},
    ecs::schedule::{common_conditions::not, IntoSystemConfigs},
};

fn main() {
    App::new()
        .add_systems(
            Startup,
            (
                print_a.run_if(always_true),
                print_b.run_if(not(always_true)),
            ),
        )
        .run();
}

fn print_a() {
    println!("A");
}

fn print_b() {
    println!("B");
}

fn always_true() -> bool {
    return true;
}

Output:

A

In the output, only A is printed, and B is not printed. This means print_a is executed, but print_b is not.

➡️ Next: Running A System By An Event

📘 Back: Table of contents