Skip to content

Commit

Permalink
Rename state_equals condition to in_state (#7677)
Browse files Browse the repository at this point in the history
# Objective

- Improve readability of the run condition for systems only running in a certain state

## Solution

- Rename `state_equals` to `in_state` (see [comment by cart](#7634 (comment)) in #7634 )
- `.run_if(state_equals(variant))` now is `.run_if(in_state(variant))`

This breaks the naming pattern a bit with the related conditions `state_exists` and `state_exists_and_equals` but I could not think of better names for those and think the improved readability of `in_state` is worth it.
  • Loading branch information
NiklasEi committed Feb 14, 2023
1 parent fae61ad commit f1c0850
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ impl App {
/// These systems sets only run if the [`State<S>`] resource matches their label.
///
/// If you would like to control how other systems run based on the current state,
/// you can emulate this behavior using the [`state_equals`] [`Condition`](bevy_ecs::schedule::Condition).
/// you can emulate this behavior using the [`in_state`] [`Condition`](bevy_ecs::schedule::Condition).
///
/// Note that you can also apply state transitions at other points in the schedule
/// by adding the [`apply_state_transition`] system manually.
Expand All @@ -342,7 +342,7 @@ impl App {
main_schedule.configure_set(
OnUpdate(variant.clone())
.in_base_set(CoreSet::Update)
.run_if(state_equals(variant))
.run_if(in_state(variant))
.after(apply_state_transition::<S>),
);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/schedule/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub mod common_conditions {
/// # Panics
///
/// The condition will panic if the resource does not exist.
pub fn state_equals<S: States>(state: S) -> impl FnMut(Res<State<S>>) -> bool {
pub fn in_state<S: States>(state: S) -> impl FnMut(Res<State<S>>) -> bool {
move |current_state: Res<State<S>>| current_state.0 == state
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/schedule/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct OnExit<S: States>(pub S);
/// A [`SystemSet`] that will run within `CoreSet::Update` when this state is active.
///
/// This set, when created via `App::add_state`, is configured with both a base set and a run condition.
/// If all you want is the run condition, use the [`state_equals`](crate::schedule::common_conditions::state_equals)
/// If all you want is the run condition, use the [`in_state`](crate::schedule::common_conditions::in_state)
/// [condition](super::Condition) directly.
#[derive(SystemSet, Clone, Debug, PartialEq, Eq, Hash)]
pub struct OnUpdate<S: States>(pub S);
Expand Down

0 comments on commit f1c0850

Please sign in to comment.