From 33c31b21a967370cf9856f95cdc2d86c93d705ea Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Tue, 11 Apr 2023 20:44:20 -0400 Subject: [PATCH] only trigger a state transition when the state actually changes --- crates/bevy_ecs/src/schedule/state.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/state.rs b/crates/bevy_ecs/src/schedule/state.rs index 7bb65621e06bb..763198aa0f8e7 100644 --- a/crates/bevy_ecs/src/schedule/state.rs +++ b/crates/bevy_ecs/src/schedule/state.rs @@ -129,16 +129,18 @@ pub fn apply_state_transition(world: &mut World) { if let Some(entered) = next_state_resource.bypass_change_detection().0.take() { next_state_resource.set_changed(); - let exited = mem::replace(&mut world.resource_mut::>().0, entered.clone()); - - // Try to run the schedules if they exist. - world.try_run_schedule(OnExit(exited.clone())).ok(); - world - .try_run_schedule(OnTransition { - from: exited, - to: entered.clone(), - }) - .ok(); - world.try_run_schedule(OnEnter(entered)).ok(); + let mut state_resource = world.resource_mut::>(); + if *state_resource != entered { + let exited = mem::replace(&mut state_resource.0, entered.clone()); + // Try to run the schedules if they exist. + world.try_run_schedule(OnExit(exited.clone())).ok(); + world + .try_run_schedule(OnTransition { + from: exited, + to: entered.clone(), + }) + .ok(); + world.try_run_schedule(OnEnter(entered)).ok(); + } } }