one_shot_systems/
one_shot_systems.rs

1//! Demonstrates the use of "one-shot systems", which run once when triggered.
2//!
3//! These can be useful to help structure your logic in a push-based fashion,
4//! reducing the overhead of running extremely rarely run systems
5//! and improving schedule flexibility.
6//!
7//! See the [`World::run_system`](World::run_system) or
8//! [`World::run_system_once`](World#method.run_system_once_with)
9//! docs for more details.
10
11use bevy::{
12    ecs::system::{RunSystemOnce, SystemId},
13    prelude::*,
14};
15
16fn main() {
17    App::new()
18        .add_plugins(DefaultPlugins)
19        .add_systems(
20            Startup,
21            (
22                setup_ui,
23                setup_with_commands,
24                setup_with_world.after(setup_ui), // since we run `system_b` once in world it needs to run after `setup_ui`
25            ),
26        )
27        .add_systems(Update, (trigger_system, evaluate_callbacks).chain())
28        .run();
29}
30
31#[derive(Component)]
32struct Callback(SystemId);
33
34#[derive(Component)]
35struct Triggered;
36
37#[derive(Component)]
38struct A;
39#[derive(Component)]
40struct B;
41
42fn setup_with_commands(mut commands: Commands) {
43    let system_id = commands.register_system(system_a);
44    commands.spawn((Callback(system_id), A));
45}
46
47fn setup_with_world(world: &mut World) {
48    // We can run it once manually
49    world.run_system_once(system_b).unwrap();
50    // Or with a Callback
51    let system_id = world.register_system(system_b);
52    world.spawn((Callback(system_id), B));
53}
54
55/// Tag entities that have callbacks we want to run with the `Triggered` component.
56fn trigger_system(
57    mut commands: Commands,
58    query_a: Single<Entity, With<A>>,
59    query_b: Single<Entity, With<B>>,
60    input: Res<ButtonInput<KeyCode>>,
61) {
62    if input.just_pressed(KeyCode::KeyA) {
63        let entity = *query_a;
64        commands.entity(entity).insert(Triggered);
65    }
66    if input.just_pressed(KeyCode::KeyB) {
67        let entity = *query_b;
68        commands.entity(entity).insert(Triggered);
69    }
70}
71
72/// Runs the systems associated with each `Callback` component if the entity also has a `Triggered` component.
73///
74/// This could be done in an exclusive system rather than using `Commands` if preferred.
75fn evaluate_callbacks(query: Query<(Entity, &Callback), With<Triggered>>, mut commands: Commands) {
76    for (entity, callback) in query.iter() {
77        commands.run_system(callback.0);
78        commands.entity(entity).remove::<Triggered>();
79    }
80}
81
82fn system_a(entity_a: Single<Entity, With<Text>>, mut writer: TextUiWriter) {
83    *writer.text(*entity_a, 3) = String::from("A");
84    info!("A: One shot system registered with Commands was triggered");
85}
86
87fn system_b(entity_b: Single<Entity, With<Text>>, mut writer: TextUiWriter) {
88    *writer.text(*entity_b, 3) = String::from("B");
89    info!("B: One shot system registered with World was triggered");
90}
91
92fn setup_ui(mut commands: Commands) {
93    commands.spawn(Camera2d);
94    commands.spawn((
95        Text::default(),
96        TextLayout::new_with_justify(Justify::Center),
97        Node {
98            align_self: AlignSelf::Center,
99            justify_self: JustifySelf::Center,
100            ..default()
101        },
102        children![
103            (TextSpan::new("Press A or B to trigger a one-shot system\n")),
104            (TextSpan::new("Last Triggered: ")),
105            (
106                TextSpan::new("-"),
107                TextColor(bevy::color::palettes::css::ORANGE.into()),
108            )
109        ],
110    ));
111}