Skip to content

Latest commit

 

History

History
41 lines (30 loc) · 1.33 KB

running_a_system_by_an_event.md

File metadata and controls

41 lines (30 loc) · 1.33 KB

Running A System By An Event

We can turn on a system by an event. More precisely, we can run a system whenever a certain type of events is triggered.

This can be done by the function on_event. For example, if we consider the event KeyboardInput:

some_system.run_if(on_event::<KeyboardInput>())

In the following code, when we press any keys, the app runs a system that prints a string A key event occurred..

use bevy::{
    app::{App, Update},
    ecs::schedule::{common_conditions::on_event, IntoSystemConfigs},
    input::keyboard::KeyboardInput,
    DefaultPlugins,
};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Update, print.run_if(on_event::<KeyboardInput>()))
        .run();
}

fn print() {
    println!("A key event occurred.");
}

When a key is pressed, the string A key event occurred. is appended to the output console.

In addition to KeyboardInput, we can use any other events including custom events.

➡️ Next: Using The State Machine

📘 Back: Table of contents