Skip to content

Commit 1ffeff1

Browse files
committed
Add condition negation (#7559)
# Objective Closes #7202 ## Solution ~~Introduce a `not` helper to pipe conditions. Opened mostly for discussion. Maybe create an extension trait with `not` method? Please, advice.~~ Introduce `not(condition)` condition that inverses the result of the passed. --- ## Changelog ### Added - `not` condition.
1 parent 1b44497 commit 1ffeff1

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

crates/bevy_ecs/src/schedule/condition.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ mod sealed {
2424
}
2525

2626
pub mod common_conditions {
27-
use crate::schedule::{State, States};
28-
use crate::system::{Res, Resource};
27+
use super::Condition;
28+
use crate::{
29+
schedule::{State, States},
30+
system::{In, IntoPipeSystem, ReadOnlySystem, Res, Resource},
31+
};
2932

3033
/// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
3134
/// if the first time the condition is run and false every time after
@@ -105,4 +108,37 @@ pub mod common_conditions {
105108
None => false,
106109
}
107110
}
111+
112+
/// Generates a [`Condition`](super::Condition) that inverses the result of passed one.
113+
///
114+
/// # Examples
115+
///
116+
/// ```
117+
/// use bevy_ecs::prelude::*;
118+
/// // Building a new schedule/app...
119+
/// let mut sched = Schedule::default();
120+
/// sched.add_system(
121+
/// // This system will never run.
122+
/// my_system.run_if(not(always_true))
123+
/// )
124+
/// // ...
125+
/// # ;
126+
/// # let mut world = World::new();
127+
/// # sched.run(&mut world);
128+
///
129+
/// // A condition that always returns true.
130+
/// fn always_true() -> bool {
131+
/// true
132+
/// }
133+
/// #
134+
/// # fn my_system() { unreachable!() }
135+
/// ```
136+
pub fn not<Params, C: Condition<Params>>(
137+
condition: C,
138+
) -> impl ReadOnlySystem<In = (), Out = bool>
139+
where
140+
C::System: ReadOnlySystem,
141+
{
142+
condition.pipe(|In(val): In<bool>| !val)
143+
}
108144
}

0 commit comments

Comments
 (0)