From 5c7abb0579a48e6b0c50daedcdf554e54ef30c83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9E=97=E4=BC=9F?= Date: Tue, 4 Apr 2023 08:49:41 +0800 Subject: [PATCH] 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 --- crates/bevy_app/src/app.rs | 8 -------- crates/bevy_ecs/src/lib.rs | 2 +- crates/bevy_ecs/src/schedule/state.rs | 12 +----------- examples/2d/texture_atlas.rs | 2 +- examples/ecs/generic_system.rs | 2 +- examples/ecs/state.rs | 4 ++-- examples/games/alien_cake_addict.rs | 4 ++-- examples/games/game_menu.rs | 10 +++++----- 8 files changed, 13 insertions(+), 31 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index 8fa2d36c94039..af56109c114c7 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -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`] 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. diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index 8107de37e88a4..c8a4f3d32565e 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -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, diff --git a/crates/bevy_ecs/src/schedule/state.rs b/crates/bevy_ecs/src/schedule/state.rs index 12a001b17b99f..7424734de3820 100644 --- a/crates/bevy_ecs/src/schedule/state.rs +++ b/crates/bevy_ecs/src/schedule/state.rs @@ -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`] and [`OnExit`] schedules, /// which can be run via the [`apply_state_transition::`] 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 { 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(pub S); - /// A finite-state machine whose transitions have associated schedules /// ([`OnEnter(state)`] and [`OnExit(state)`]). /// diff --git a/examples/2d/texture_atlas.rs b/examples/2d/texture_atlas.rs index 06311236d012c..b1f6cb21137fc 100644 --- a/examples/2d/texture_atlas.rs +++ b/examples/2d/texture_atlas.rs @@ -9,7 +9,7 @@ fn main() { .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) // prevents blurry sprites .add_state::() .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(); } diff --git a/examples/ecs/generic_system.rs b/examples/ecs/generic_system.rs index ae78e55e3b2b5..aac3444855f25 100644 --- a/examples/ecs/generic_system.rs +++ b/examples/ecs/generic_system.rs @@ -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. diff --git a/examples/ecs/state.rs b/examples/ecs/state.rs index b61ba01adb79c..73af1055d78b3 100644 --- a/examples/ecs/state.rs +++ b/examples/ecs/state.rs @@ -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` 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(); } diff --git a/examples/games/alien_cake_addict.rs b/examples/games/alien_cake_addict.rs index ed9fb30529154..47a16d04d7a55 100644 --- a/examples/games/alien_cake_addict.rs +++ b/examples/games/alien_cake_addict.rs @@ -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, ), ) diff --git a/examples/games/game_menu.rs b/examples/games/game_menu.rs index 3e38e34f385a1..bc2c3d7c00d2c 100644 --- a/examples/games/game_menu.rs +++ b/examples/games/game_menu.rs @@ -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::); } @@ -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::); } } @@ -284,7 +284,7 @@ mod menu { Update, ( setting_button:: - .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::.in_set(OnUpdate(MenuState::SettingsSound)), + setting_button::.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)), ); } }