Remove OnUpdate system set (#8260)

# Objective

- Fixes https://github.com/bevyengine/bevy/issues/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>
This commit is contained in:
张林伟 2023-04-04 08:49:41 +08:00 committed by GitHub
parent b423e6ee15
commit 5c7abb0579
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 13 additions and 31 deletions

View file

@ -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).
///
@ -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.

View file

@ -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,

View file

@ -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;
@ -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
///
@ -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)`]).
///

View file

@ -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();
}

View file

@ -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.

View file

@ -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();
}

View file

@ -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,
),
)

View file

@ -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>);
}
@ -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>);
}
}
@ -284,7 +284,7 @@ mod menu {
Update,
(
setting_button::<DisplayQuality>
.in_set(OnUpdate(MenuState::SettingsDisplay)),
.run_if(in_state(MenuState::SettingsDisplay)),
),
)
.add_systems(
@ -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),
@ -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)),
);
}
}