Skip to content

Commit 0a9c469

Browse files
Remove .on_update method to improve API consistency and clarity (#7667)
# Objective Fixes #7632. As discussed in #7634, it can be quite challenging for users to intuit the mental model of how states now work. ## Solution Rather than change the behavior of the `OnUpdate` system set, instead work on making sure it's easy to understand what's going on. Two things have been done: 1. Remove the `.on_update` method from our bevy of system building traits. This was special-cased and made states feel much more magical than they need to. 2. Improve the docs for the `OnUpdate` system set.
1 parent 17ed41d commit 0a9c469

File tree

8 files changed

+19
-54
lines changed

8 files changed

+19
-54
lines changed

crates/bevy_app/src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl App {
317317
/// initial state.
318318
///
319319
/// This also adds an [`OnUpdate`] system set for each state variant,
320-
/// which run during [`CoreSet::StateTransitions`] after the transitions are applied.
320+
/// which run during [`CoreSet::Update`] after the transitions are applied.
321321
/// These systems sets only run if the [`State<S>`] resource matches their label.
322322
///
323323
/// If you would like to control how other systems run based on the current state,
@@ -341,7 +341,7 @@ impl App {
341341
for variant in S::variants() {
342342
main_schedule.configure_set(
343343
OnUpdate(variant.clone())
344-
.in_base_set(CoreSet::StateTransitions)
344+
.in_base_set(CoreSet::Update)
345345
.run_if(state_equals(variant))
346346
.after(apply_state_transition::<S>),
347347
);

crates/bevy_ecs/src/schedule/config.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::{
55
condition::{BoxedCondition, Condition},
66
graph_utils::{Ambiguity, Dependency, DependencyKind, GraphInfo},
77
set::{BoxedSystemSet, IntoSystemSet, SystemSet},
8-
state::{OnUpdate, States},
98
},
109
system::{BoxedSystem, IntoSystem, System},
1110
};
@@ -102,8 +101,6 @@ pub trait IntoSystemSetConfig: sealed::IntoSystemSetConfig {
102101
/// The `Condition` will be evaluated at most once (per schedule run),
103102
/// the first time a system in this set prepares to run.
104103
fn run_if<P>(self, condition: impl Condition<P>) -> SystemSetConfig;
105-
/// Add this set to the [`OnUpdate(state)`](OnUpdate) set.
106-
fn on_update(self, state: impl States) -> SystemSetConfig;
107104
/// Suppress warnings and errors that would result from systems in this set having ambiguities
108105
/// (conflicting access but indeterminate order) with systems in `set`.
109106
fn ambiguous_with<M>(self, set: impl IntoSystemSet<M>) -> SystemSetConfig;
@@ -146,10 +143,6 @@ where
146143
self.into_config().run_if(condition)
147144
}
148145

149-
fn on_update(self, state: impl States) -> SystemSetConfig {
150-
self.into_config().on_update(state)
151-
}
152-
153146
fn ambiguous_with<M>(self, set: impl IntoSystemSet<M>) -> SystemSetConfig {
154147
self.into_config().ambiguous_with(set)
155148
}
@@ -190,10 +183,6 @@ impl IntoSystemSetConfig for BoxedSystemSet {
190183
self.into_config().run_if(condition)
191184
}
192185

193-
fn on_update(self, state: impl States) -> SystemSetConfig {
194-
self.into_config().on_update(state)
195-
}
196-
197186
fn ambiguous_with<M>(self, set: impl IntoSystemSet<M>) -> SystemSetConfig {
198187
self.into_config().ambiguous_with(set)
199188
}
@@ -270,10 +259,6 @@ impl IntoSystemSetConfig for SystemSetConfig {
270259
self
271260
}
272261

273-
fn on_update(self, state: impl States) -> SystemSetConfig {
274-
self.in_set(OnUpdate(state))
275-
}
276-
277262
fn ambiguous_with<M>(mut self, set: impl IntoSystemSet<M>) -> Self {
278263
ambiguous_with(&mut self.graph_info, Box::new(set.into_system_set()));
279264
self
@@ -310,8 +295,6 @@ pub trait IntoSystemConfig<Params>: sealed::IntoSystemConfig<Params> {
310295
/// The `Condition` will be evaluated at most once (per schedule run),
311296
/// when the system prepares to run.
312297
fn run_if<P>(self, condition: impl Condition<P>) -> SystemConfig;
313-
/// Add this system to the [`OnUpdate(state)`](OnUpdate) set.
314-
fn on_update(self, state: impl States) -> SystemConfig;
315298
/// Suppress warnings and errors that would result from this system having ambiguities
316299
/// (conflicting access but indeterminate order) with systems in `set`.
317300
fn ambiguous_with<M>(self, set: impl IntoSystemSet<M>) -> SystemConfig;
@@ -354,10 +337,6 @@ where
354337
self.into_config().run_if(condition)
355338
}
356339

357-
fn on_update(self, state: impl States) -> SystemConfig {
358-
self.into_config().on_update(state)
359-
}
360-
361340
fn ambiguous_with<M>(self, set: impl IntoSystemSet<M>) -> SystemConfig {
362341
self.into_config().ambiguous_with(set)
363342
}
@@ -398,10 +377,6 @@ impl IntoSystemConfig<()> for BoxedSystem<(), ()> {
398377
self.into_config().run_if(condition)
399378
}
400379

401-
fn on_update(self, state: impl States) -> SystemConfig {
402-
self.into_config().on_update(state)
403-
}
404-
405380
fn ambiguous_with<M>(self, set: impl IntoSystemSet<M>) -> SystemConfig {
406381
self.into_config().ambiguous_with(set)
407382
}
@@ -470,10 +445,6 @@ impl IntoSystemConfig<()> for SystemConfig {
470445
self
471446
}
472447

473-
fn on_update(self, state: impl States) -> Self {
474-
self.in_set(OnUpdate(state))
475-
}
476-
477448
fn ambiguous_with<M>(mut self, set: impl IntoSystemSet<M>) -> Self {
478449
ambiguous_with(&mut self.graph_info, Box::new(set.into_system_set()));
479450
self
@@ -549,11 +520,6 @@ where
549520
self.into_configs().after(set)
550521
}
551522

552-
/// Add this set to the [`OnUpdate(state)`](OnUpdate) set.
553-
fn on_update(self, state: impl States) -> SystemConfigs {
554-
self.into_configs().on_update(state)
555-
}
556-
557523
/// Suppress warnings and errors that would result from these systems having ambiguities
558524
/// (conflicting access but indeterminate order) with systems in `set`.
559525
fn ambiguous_with<M>(self, set: impl IntoSystemSet<M>) -> SystemConfigs {
@@ -654,10 +620,6 @@ impl IntoSystemConfigs<()> for SystemConfigs {
654620
self
655621
}
656622

657-
fn on_update(self, state: impl States) -> Self {
658-
self.in_set(OnUpdate(state))
659-
}
660-
661623
fn chain(mut self) -> Self {
662624
self.chained = true;
663625
self

crates/bevy_ecs/src/schedule/state.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ pub struct OnEnter<S: States>(pub S);
5353
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)]
5454
pub struct OnExit<S: States>(pub S);
5555

56-
/// A [`SystemSet`] that will run within `CoreSet::StateTransitions` when this state is active.
56+
/// A [`SystemSet`] that will run within `CoreSet::Update` when this state is active.
5757
///
58-
/// This is provided for convenience. A more general [`state_equals`](crate::schedule::common_conditions::state_equals)
59-
/// [condition](super::Condition) also exists for systems that need to run elsewhere.
58+
/// This set, when created via `App::add_state`, is configured with both a base set and a run condition.
59+
/// If all you want is the run condition, use the [`state_equals`](crate::schedule::common_conditions::state_equals)
60+
/// [condition](super::Condition) directly.
6061
#[derive(SystemSet, Clone, Debug, PartialEq, Eq, Hash)]
6162
pub struct OnUpdate<S: States>(pub S);
6263

examples/2d/texture_atlas.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) // prevents blurry sprites
1010
.add_state::<AppState>()
1111
.add_system_to_schedule(OnEnter(AppState::Setup), load_textures)
12-
.add_system(check_textures.on_update(AppState::Setup))
12+
.add_system(check_textures.in_set(OnUpdate(AppState::Setup)))
1313
.add_system_to_schedule(OnEnter(AppState::Finished), setup)
1414
.run();
1515
}

examples/ecs/generic_system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn main() {
4444
.add_state::<AppState>()
4545
.add_startup_system(setup_system)
4646
.add_system(print_text_system)
47-
.add_system(transition_to_in_game_system.on_update(AppState::MainMenu))
47+
.add_system(transition_to_in_game_system.in_set(OnUpdate(AppState::MainMenu)))
4848
// add the cleanup systems
4949
.add_system_to_schedule(
5050
OnExit(AppState::MainMenu),

examples/ecs/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ fn main() {
1818
.add_system_to_schedule(OnEnter(AppState::Menu), setup_menu)
1919
// By contrast, on_update systems are stored in the main schedule, during CoreSet::Update,
2020
// and simply check the value of the `State<T>` resource to see if they should run each frame.
21-
.add_system(menu.on_update(AppState::Menu))
21+
.add_system(menu.in_set(OnUpdate(AppState::Menu)))
2222
.add_system_to_schedule(OnExit(AppState::Menu), cleanup_menu)
2323
.add_system_to_schedule(OnEnter(AppState::InGame), setup_game)
24-
.add_systems((movement, change_color).on_update(AppState::InGame))
24+
.add_systems((movement, change_color).in_set(OnUpdate(AppState::InGame)))
2525
.run();
2626
}
2727

examples/games/alien_cake_addict.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ fn main() {
3434
scoreboard_system,
3535
spawn_bonus,
3636
)
37-
.on_update(GameState::Playing),
37+
.in_set(OnUpdate(GameState::Playing)),
3838
)
3939
.add_system_to_schedule(OnExit(GameState::Playing), teardown)
4040
.add_system_to_schedule(OnEnter(GameState::GameOver), display_score)
41-
.add_system(gameover_keyboard.on_update(GameState::GameOver))
41+
.add_system(gameover_keyboard.in_set(OnUpdate(GameState::GameOver)))
4242
.add_system_to_schedule(OnExit(GameState::GameOver), teardown)
4343
.add_system(bevy::window::close_on_esc)
4444
.run();

examples/games/game_menu.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ mod splash {
6262
// When entering the state, spawn everything needed for this screen
6363
.add_system_to_schedule(OnEnter(GameState::Splash), splash_setup)
6464
// While in this state, run the `countdown` system
65-
.add_system(countdown.on_update(GameState::Splash))
65+
.add_system(countdown.in_set(OnUpdate(GameState::Splash)))
6666
// When exiting the state, despawn everything that was spawned for this screen
6767
.add_system_to_schedule(
6868
OnExit(GameState::Splash),
@@ -134,7 +134,7 @@ mod game {
134134
impl Plugin for GamePlugin {
135135
fn build(&self, app: &mut App) {
136136
app.add_system_to_schedule(OnEnter(GameState::Game), game_setup)
137-
.add_system(game.on_update(GameState::Game))
137+
.add_system(game.in_set(OnUpdate(GameState::Game)))
138138
.add_system_to_schedule(OnExit(GameState::Game), despawn_screen::<OnGameScreen>);
139139
}
140140
}
@@ -283,7 +283,9 @@ mod menu {
283283
OnEnter(MenuState::SettingsDisplay),
284284
display_settings_menu_setup,
285285
)
286-
.add_system(setting_button::<DisplayQuality>.on_update(MenuState::SettingsDisplay))
286+
.add_system(
287+
setting_button::<DisplayQuality>.in_set(OnUpdate(MenuState::SettingsDisplay)),
288+
)
287289
.add_system_to_schedule(
288290
OnExit(MenuState::SettingsDisplay),
289291
despawn_screen::<OnDisplaySettingsMenuScreen>,
@@ -293,13 +295,13 @@ mod menu {
293295
OnEnter(MenuState::SettingsSound),
294296
sound_settings_menu_setup,
295297
)
296-
.add_system(setting_button::<Volume>.on_update(MenuState::SettingsSound))
298+
.add_system(setting_button::<Volume>.in_set(OnUpdate(MenuState::SettingsSound)))
297299
.add_system_to_schedule(
298300
OnExit(MenuState::SettingsSound),
299301
despawn_screen::<OnSoundSettingsMenuScreen>,
300302
)
301303
// Common systems to all screens that handles buttons behaviour
302-
.add_systems((menu_action, button_system).on_update(GameState::Menu));
304+
.add_systems((menu_action, button_system).in_set(OnUpdate(GameState::Menu)));
303305
}
304306
}
305307

0 commit comments

Comments
 (0)