Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about missing StatesPlugin when installing states #13877

Merged
merged 2 commits into from
Jun 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ensure_states_plugin_is_installed(app: &SubApp) {
MiniaczQ marked this conversation as resolved.
Show resolved Hide resolved
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 {
ensure_states_plugin_is_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 {
ensure_states_plugin_is_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 {
ensure_states_plugin_is_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 {
ensure_states_plugin_is_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