Skip to content

Commit

Permalink
Warn about missing StatesPlugin when installing states (#13877)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
MiniaczQ authored Jun 16, 2024
1 parent f691173 commit 90894a1
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions crates/bevy_state/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -57,8 +57,16 @@ pub trait AppExtStates {
fn enable_state_scoped_entities<S: States>(&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::<StatesPlugin>() {
warn_once!("States were added to the app, but `StatesPlugin` is not installed.");
}
}

impl AppExtStates for SubApp {
fn init_state<S: FreelyMutableState + FromWorld>(&mut self) -> &mut Self {
warn_if_no_states_plugin_installed(self);
if !self.world().contains_resource::<State<S>>() {
setup_state_transitions_in_world(self.world_mut(), Some(Startup.intern()));
self.init_resource::<State<S>>()
Expand All @@ -80,6 +88,7 @@ impl AppExtStates for SubApp {
}

fn insert_state<S: FreelyMutableState>(&mut self, state: S) -> &mut Self {
warn_if_no_states_plugin_installed(self);
if !self.world().contains_resource::<State<S>>() {
setup_state_transitions_in_world(self.world_mut(), Some(Startup.intern()));
self.insert_resource::<State<S>>(State::new(state.clone()))
Expand Down Expand Up @@ -107,6 +116,7 @@ impl AppExtStates for SubApp {
}

fn add_computed_state<S: ComputedStates>(&mut self) -> &mut Self {
warn_if_no_states_plugin_installed(self);
if !self
.world()
.contains_resource::<Events<StateTransitionEvent<S>>>()
Expand All @@ -132,6 +142,7 @@ impl AppExtStates for SubApp {
}

fn add_sub_state<S: SubStates>(&mut self) -> &mut Self {
warn_if_no_states_plugin_installed(self);
if !self
.world()
.contains_resource::<Events<StateTransitionEvent<S>>>()
Expand All @@ -158,8 +169,6 @@ impl AppExtStates for SubApp {
}

fn enable_state_scoped_entities<S: States>(&mut self) -> &mut Self {
use bevy_utils::tracing::warn;

if !self
.world()
.contains_resource::<Events<StateTransitionEvent<S>>>()
Expand Down

0 comments on commit 90894a1

Please sign in to comment.