Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add condition negation #7559

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions crates/bevy_ecs/src/schedule/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ mod sealed {
}

pub mod common_conditions {
use crate::schedule::{State, States};
use crate::system::{Res, Resource};
use super::Condition;
use crate::{
schedule::{State, States},
system::{In, IntoPipeSystem, ReadOnlySystem, Res, Resource},
};

/// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
/// if the first time the condition is run and false every time after
Expand Down Expand Up @@ -105,4 +108,37 @@ pub mod common_conditions {
None => false,
}
}

/// Generates a [`Condition`](super::Condition) that inverses the result of passed one.
///
/// # Examples
///
/// ```
/// use bevy_ecs::prelude::*;
/// // Building a new schedule/app...
/// let mut sched = Schedule::default();
/// sched.add_system(
/// // This system will never run.
/// my_system.run_if(not(always_true))
/// )
/// // ...
/// # ;
/// # let mut world = World::new();
/// # sched.run(&mut world);
///
/// // A condition that always returns true.
/// fn always_true() -> bool {
/// true
/// }
/// #
/// # fn my_system() { unreachable!() }
/// ```
pub fn not<Params, C: Condition<Params>>(
condition: C,
) -> impl ReadOnlySystem<In = (), Out = bool>
where
C::System: ReadOnlySystem,
{
condition.pipe(|In(val): In<bool>| !val)
}
}