diff --git a/crates/bevy_state/src/app.rs b/crates/bevy_state/src/app.rs index 33a141e8d6be9..fa9c27fba9d2c 100644 --- a/crates/bevy_state/src/app.rs +++ b/crates/bevy_state/src/app.rs @@ -4,7 +4,7 @@ use bevy_ecs::{ schedule::{IntoSystemConfigs, ScheduleLabel}, world::FromWorld, }; -use bevy_utils::tracing::warn; +use bevy_utils::{tracing::warn, warn_once}; use crate::state::{ setup_state_transitions_in_world, ComputedStates, FreelyMutableState, NextState, State, @@ -57,8 +57,16 @@ pub trait AppExtStates { fn enable_state_scoped_entities(&mut self) -> &mut Self; } +/// Separate function to only warn once for all state installation methods. +fn warn_if_no_states_plugin_installed(app: &SubApp) { + if !app.is_plugin_added::() { + warn_once!("States were added to the app, but `StatesPlugin` is not installed."); + } +} + impl AppExtStates for SubApp { fn init_state(&mut self) -> &mut Self { + warn_if_no_states_plugin_installed(self); if !self.world().contains_resource::>() { setup_state_transitions_in_world(self.world_mut(), Some(Startup.intern())); self.init_resource::>() @@ -80,6 +88,7 @@ impl AppExtStates for SubApp { } fn insert_state(&mut self, state: S) -> &mut Self { + warn_if_no_states_plugin_installed(self); if !self.world().contains_resource::>() { setup_state_transitions_in_world(self.world_mut(), Some(Startup.intern())); self.insert_resource::>(State::new(state.clone())) @@ -107,6 +116,7 @@ impl AppExtStates for SubApp { } fn add_computed_state(&mut self) -> &mut Self { + warn_if_no_states_plugin_installed(self); if !self .world() .contains_resource::>>() @@ -132,6 +142,7 @@ impl AppExtStates for SubApp { } fn add_sub_state(&mut self) -> &mut Self { + warn_if_no_states_plugin_installed(self); if !self .world() .contains_resource::>>() @@ -158,8 +169,6 @@ impl AppExtStates for SubApp { } fn enable_state_scoped_entities(&mut self) -> &mut Self { - use bevy_utils::tracing::warn; - if !self .world() .contains_resource::>>()