From 90894a1910897a309f9e3d88b0466be2c4bb2562 Mon Sep 17 00:00:00 2001 From: MiniaczQ Date: Sun, 16 Jun 2024 19:36:24 +0200 Subject: [PATCH] Warn about missing `StatesPlugin` when installing states (#13877) # Objective - Fixes #13874 ## Solution - Confirm that the `StatesPlugin` is installed when trying to add states. - Skipped for state scoped entities, since those will warn about missing states. --- crates/bevy_state/src/app.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) 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::>>()