docs/bevy/examples/ecs/run_conditions.rs 5.1 K · 107 lines · raw · history
| 1 | //! This example demonstrates how to use run conditions to control when systems run. |
| 2 | |
| 3 | use bevy::prelude::*; |
| 4 | |
| 5 | fn main() { |
| 6 | println!(); |
| 7 | println!("For the first 2 seconds you will not be able to increment the counter"); |
| 8 | println!("Once that time has passed you can press space, enter, left mouse, right mouse or touch the screen to increment the counter"); |
| 9 | println!(); |
| 10 | |
| 11 | App::new() |
| 12 | .add_plugins(DefaultPlugins) |
| 13 | .init_resource::<InputCounter>() |
| 14 | .add_systems( |
| 15 | Update, |
| 16 | ( |
| 17 | increment_input_counter |
| 18 | // The common_conditions module has a few useful run conditions |
| 19 | // for checking resources and states. These are included in the prelude. |
| 20 | .run_if(resource_exists::<InputCounter>) |
| 21 | // `.or_else()` is a run condition combinator that only evaluates the second condition |
| 22 | // if the first condition returns `false`. This behavior is known as "short-circuiting", |
| 23 | // and is how the `||` operator works in Rust (as well as most C-family languages). |
| 24 | // In this case, the `has_user_input` run condition will be evaluated since the `Unused` resource has not been initialized. |
| 25 | .run_if(resource_exists::<Unused>.or_else( |
| 26 | // This is a custom run condition, defined using a system that returns |
| 27 | // a `bool` and which has read-only `SystemParam`s. |
| 28 | // Only a single run condition must return `true` in order for the system to run. |
| 29 | has_user_input, |
| 30 | )), |
| 31 | print_input_counter |
| 32 | // `.and_then()` is a run condition combinator that only evaluates the second condition |
| 33 | // if the first condition returns `true`, analogous to the `&&` operator. |
| 34 | // In this case, the short-circuiting behavior prevents the second run condition from |
| 35 | // panicking if the `InputCounter` resource has not been initialized. |
| 36 | .run_if(resource_exists::<InputCounter>.and_then( |
| 37 | // This is a custom run condition in the form of a closure. |
| 38 | // This is useful for small, simple run conditions you don't need to reuse. |
| 39 | // All the normal rules still apply: all parameters must be read only except for local parameters. |
| 40 | |counter: Res<InputCounter>| counter.is_changed() && !counter.is_added(), |
| 41 | )), |
| 42 | print_time_message |
| 43 | // This function returns a custom run condition, much like the common conditions module. |
| 44 | // It will only return true once 2 seconds have passed. |
| 45 | .run_if(time_passed(2.0)) |
| 46 | // You can use the `not` condition from the common_conditions module |
| 47 | // to inverse a run condition. In this case it will return true if |
| 48 | // less than 2.5 seconds have elapsed since the app started. |
| 49 | .run_if(not(time_passed(2.5))), |
| 50 | ), |
| 51 | ) |
| 52 | .run(); |
| 53 | } |
| 54 | |
| 55 | #[derive(Resource, Default)] |
| 56 | struct InputCounter(usize); |
| 57 | |
| 58 | #[derive(Resource)] |
| 59 | struct Unused; |
| 60 | |
| 61 | /// Return true if any of the defined inputs were just pressed. |
| 62 | /// |
| 63 | /// This is a custom run condition, it can take any normal system parameters as long as |
| 64 | /// they are read only (except for local parameters which can be mutable). |
| 65 | /// It returns a bool which determines if the system should run. |
| 66 | fn has_user_input( |
| 67 | keyboard_input: Res<ButtonInput<KeyCode>>, |
| 68 | mouse_button_input: Res<ButtonInput<MouseButton>>, |
| 69 | touch_input: Res<Touches>, |
| 70 | ) -> bool { |
| 71 | keyboard_input.just_pressed(KeyCode::Space) |
| 72 | || keyboard_input.just_pressed(KeyCode::Enter) |
| 73 | || mouse_button_input.just_pressed(MouseButton::Left) |
| 74 | || mouse_button_input.just_pressed(MouseButton::Right) |
| 75 | || touch_input.any_just_pressed() |
| 76 | } |
| 77 | |
| 78 | /// This is a function that returns a closure which can be used as a run condition. |
| 79 | /// |
| 80 | /// This is useful because you can reuse the same run condition but with different variables. |
| 81 | /// This is how the common conditions module works. |
| 82 | fn time_passed(t: f32) -> impl FnMut(Local<f32>, Res<Time>) -> bool { |
| 83 | move |mut timer: Local<f32>, time: Res<Time>| { |
| 84 | // Tick the timer |
| 85 | *timer += time.delta_secs(); |
| 86 | // Return true if the timer has passed the time |
| 87 | *timer >= t |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// SYSTEM: Increment the input counter |
| 92 | /// Notice how we can take just the `ResMut` and not have to wrap |
| 93 | /// it in an option in case it hasn't been initialized, this is because |
| 94 | /// it has a run condition that checks if the `InputCounter` resource exists |
| 95 | fn increment_input_counter(mut counter: ResMut<InputCounter>) { |
| 96 | counter.0 += 1; |
| 97 | } |
| 98 | |
| 99 | /// SYSTEM: Print the input counter |
| 100 | fn print_input_counter(counter: Res<InputCounter>) { |
| 101 | println!("Input counter: {}", counter.0); |
| 102 | } |
| 103 | |
| 104 | /// SYSTEM: Adds the input counter resource |
| 105 | fn print_time_message() { |
| 106 | println!("It has been more than 2 seconds since the program started and less than 2.5 seconds"); |
| 107 | } |