[go: nahoru, domu]

Skip to content

Commit

Permalink
Remove OnUpdate system set (#8260)
Browse files Browse the repository at this point in the history
# Objective

- Fixes #8239.

## Solution

- Replace `OnUpdate` with `run_if(in_state(xxx))`.

---

## Migration Guide

- Replace `OnUpdate` with `run_if(in_state(xxx))`.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
  • Loading branch information
lewiszlw and alice-i-cecile committed Apr 4, 2023
1 parent b423e6e commit 5c7abb0
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 31 deletions.
8 changes: 0 additions & 8 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,6 @@ impl App {
/// [`run_once`](`run_once_condition`) condition to run the on enter schedule of the
/// initial state.
///
/// This also adds an [`OnUpdate`] system set for each state variant,
/// which runs during [`Update`] after the transitions are applied.
/// These system sets only run if the [`State<S>`] resource matches the respective state variant.
///
/// If you would like to control how other systems run based on the current state,
/// you can emulate this behavior using the [`in_state`] [`Condition`](bevy_ecs::schedule::Condition).
///
Expand All @@ -333,10 +329,6 @@ impl App {
.chain(),
);

for variant in S::variants() {
self.configure_set(Update, OnUpdate(variant.clone()).run_if(in_state(variant)));
}

// The OnEnter, OnExit, and OnTransition schedules are lazily initialized
// (i.e. when the first system is added to them), and World::try_run_schedule is used to fail
// gracefully if they aren't present.
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub mod prelude {
schedule::{
apply_state_transition, apply_system_buffers, common_conditions::*, Condition,
IntoSystemConfigs, IntoSystemSet, IntoSystemSetConfig, IntoSystemSetConfigs, NextState,
OnEnter, OnExit, OnTransition, OnUpdate, Schedule, Schedules, State, States, SystemSet,
OnEnter, OnExit, OnTransition, Schedule, Schedules, State, States, SystemSet,
},
system::{
adapter as system_adapter,
Expand Down
12 changes: 1 addition & 11 deletions crates/bevy_ecs/src/schedule/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::mem;

use crate as bevy_ecs;
use crate::change_detection::DetectChangesMut;
use crate::schedule::{ScheduleLabel, SystemSet};
use crate::schedule::ScheduleLabel;
use crate::system::Resource;
use crate::world::World;

Expand All @@ -20,8 +20,6 @@ pub use bevy_ecs_macros::States;
///
/// State transitions typically occur in the [`OnEnter<T::Variant>`] and [`OnExit<T:Variant>`] schedules,
/// which can be run via the [`apply_state_transition::<T>`] system.
/// Systems that run each frame in various states are typically stored in the main schedule,
/// and are conventionally part of the [`OnUpdate(T::Variant)`] system set.
///
/// # Example
///
Expand Down Expand Up @@ -66,14 +64,6 @@ pub struct OnTransition<S: States> {
pub to: 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 a run condition.
/// 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);

/// A finite-state machine whose transitions have associated schedules
/// ([`OnEnter(state)`] and [`OnExit(state)`]).
///
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) // prevents blurry sprites
.add_state::<AppState>()
.add_systems(OnEnter(AppState::Setup), load_textures)
.add_systems(Update, check_textures.in_set(OnUpdate(AppState::Setup)))
.add_systems(Update, check_textures.run_if(in_state(AppState::Setup)))
.add_systems(OnEnter(AppState::Finished), setup)
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/generic_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() {
Update,
(
print_text_system,
transition_to_in_game_system.in_set(OnUpdate(AppState::MainMenu)),
transition_to_in_game_system.run_if(in_state(AppState::MainMenu)),
),
)
// Cleanup systems.
Expand Down
4 changes: 2 additions & 2 deletions examples/ecs/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ fn main() {
.add_systems(OnEnter(AppState::Menu), setup_menu)
// By contrast, update systems are stored in the `Update` schedule. They simply
// check the value of the `State<T>` resource to see if they should run each frame.
.add_systems(Update, menu.in_set(OnUpdate(AppState::Menu)))
.add_systems(Update, menu.run_if(in_state(AppState::Menu)))
.add_systems(OnExit(AppState::Menu), cleanup_menu)
.add_systems(OnEnter(AppState::InGame), setup_game)
.add_systems(
Update,
(movement, change_color).in_set(OnUpdate(AppState::InGame)),
(movement, change_color).run_if(in_state(AppState::InGame)),
)
.run();
}
Expand Down
4 changes: 2 additions & 2 deletions examples/games/alien_cake_addict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ fn main() {
scoreboard_system,
spawn_bonus,
)
.in_set(OnUpdate(GameState::Playing)),
.run_if(in_state(GameState::Playing)),
)
.add_systems(OnExit(GameState::Playing), teardown)
.add_systems(OnEnter(GameState::GameOver), display_score)
.add_systems(
Update,
(
gameover_keyboard.in_set(OnUpdate(GameState::GameOver)),
gameover_keyboard.run_if(in_state(GameState::GameOver)),
bevy::window::close_on_esc,
),
)
Expand Down
10 changes: 5 additions & 5 deletions examples/games/game_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod splash {
// When entering the state, spawn everything needed for this screen
.add_systems(OnEnter(GameState::Splash), splash_setup)
// While in this state, run the `countdown` system
.add_systems(Update, countdown.in_set(OnUpdate(GameState::Splash)))
.add_systems(Update, countdown.run_if(in_state(GameState::Splash)))
// When exiting the state, despawn everything that was spawned for this screen
.add_systems(OnExit(GameState::Splash), despawn_screen::<OnSplashScreen>);
}
Expand Down Expand Up @@ -131,7 +131,7 @@ mod game {
impl Plugin for GamePlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(GameState::Game), game_setup)
.add_systems(Update, game.in_set(OnUpdate(GameState::Game)))
.add_systems(Update, game.run_if(in_state(GameState::Game)))
.add_systems(OnExit(GameState::Game), despawn_screen::<OnGameScreen>);
}
}
Expand Down Expand Up @@ -284,7 +284,7 @@ mod menu {
Update,
(
setting_button::<DisplayQuality>
.in_set(OnUpdate(MenuState::SettingsDisplay)),
.run_if(in_state(MenuState::SettingsDisplay)),
),
)
.add_systems(
Expand All @@ -295,7 +295,7 @@ mod menu {
.add_systems(OnEnter(MenuState::SettingsSound), sound_settings_menu_setup)
.add_systems(
Update,
setting_button::<Volume>.in_set(OnUpdate(MenuState::SettingsSound)),
setting_button::<Volume>.run_if(in_state(MenuState::SettingsSound)),
)
.add_systems(
OnExit(MenuState::SettingsSound),
Expand All @@ -304,7 +304,7 @@ mod menu {
// Common systems to all screens that handles buttons behaviour
.add_systems(
Update,
(menu_action, button_system).in_set(OnUpdate(GameState::Menu)),
(menu_action, button_system).run_if(in_state(GameState::Menu)),
);
}
}
Expand Down

0 comments on commit 5c7abb0

Please sign in to comment.