pub fn not<Marker, TOut, T>(
condition: T,
) -> AdapterSystem<NotMarker, <T as IntoSystem<(), TOut, Marker>>::System>Expand description
Generates a SystemCondition that inverses the result of passed one.
ยงExample
app.add_systems(
// `not` will inverse any condition you pass in.
// Since the condition we choose always returns true
// this system will never run
my_system.run_if(not(always)),
);
fn my_system(mut counter: ResMut<Counter>) {
counter.0 += 1;
}
fn always() -> bool {
true
}
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 0);Examples found in repository?
examples/shader/gpu_readback.rs (line 59)
45 fn build(&self, app: &mut App) {
46 let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
47 return;
48 };
49 render_app
50 .add_systems(
51 RenderStartup,
52 (init_compute_pipeline, add_compute_render_graph_node),
53 )
54 .add_systems(
55 Render,
56 prepare_bind_group
57 .in_set(RenderSystems::PrepareBindGroups)
58 // We don't need to recreate the bind group every frame
59 .run_if(not(resource_exists::<GpuBufferBindGroup>)),
60 );
61 }More examples
examples/games/stepping.rs (line 63)
34 fn build(&self, app: &mut App) {
35 app.add_systems(Startup, build_stepping_hint);
36 if cfg!(not(feature = "bevy_debug_stepping")) {
37 return;
38 }
39
40 // create and insert our debug schedule into the main schedule order.
41 // We need an independent schedule so we have access to all other
42 // schedules through the `Stepping` resource
43 app.init_schedule(DebugSchedule);
44 let mut order = app.world_mut().resource_mut::<MainScheduleOrder>();
45 order.insert_after(Update, DebugSchedule);
46
47 // create our stepping resource
48 let mut stepping = Stepping::new();
49 for label in &self.schedule_labels {
50 stepping.add_schedule(*label);
51 }
52 app.insert_resource(stepping);
53
54 // add our startup & stepping systems
55 app.insert_resource(State {
56 ui_top: self.top,
57 ui_left: self.left,
58 systems: Vec::new(),
59 })
60 .add_systems(
61 DebugSchedule,
62 (
63 build_ui.run_if(not(initialized)),
64 handle_input,
65 update_ui.run_if(initialized),
66 )
67 .chain(),
68 );
69 }examples/ecs/run_conditions.rs (line 49)
5fn 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()` 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(
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()` 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(
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}