#![no_std]12//! In Bevy, states are app-wide interdependent, finite state machines that are generally used to model the large scale structure of your program: whether a game is paused, if the player is in combat, if assets are loaded and so on.3//!4//! This module provides 3 distinct types of state, all of which implement the [`States`](state::States) trait:5//!6//! - Standard [`States`](state::States) can only be changed by manually setting the [`NextState<S>`](state::NextState) resource.7//! These states are the baseline on which the other state types are built, and can be used on8//! their own for many simple patterns. See the [states example](https://github.com/bevyengine/bevy/blob/latest/examples/state/states.rs)9//! for a simple use case.10//! - [`SubStates`](state::SubStates) are children of other states - they can be changed manually using [`NextState<S>`](state::NextState),11//! but are removed from the [`World`](bevy_ecs::prelude::World) if the source states aren't in the right state. See the [sub_states example](https://github.com/bevyengine/bevy/blob/latest/examples/state/sub_states.rs)12//! for a simple use case based on the derive macro, or read the trait docs for more complex scenarios.13//! - [`ComputedStates`](state::ComputedStates) are fully derived from other states - they provide a [`compute`](state::ComputedStates::compute) method14//! that takes in the source states and returns their derived value. They are particularly useful for situations15//! where a simplified view of the source states is necessary - such as having an `InAMenu` computed state, derived16//! from a source state that defines multiple distinct menus. See the [computed state example](https://github.com/bevyengine/bevy/blob/latest/examples/state/computed_states.rs)17//! to see usage samples for these states.18//!19//! Most of the utilities around state involve running systems during transitions between states, or20//! determining whether to run certain systems, though they can be used more directly as well. This21//! makes it easier to transition between menus, add loading screens, pause games, and more.22//!23//! Specifically, Bevy provides the following utilities:24//!25//! - 3 Transition Schedules - [`OnEnter<S>`](crate::state::OnEnter), [`OnExit<S>`](crate::state::OnExit) and [`OnTransition<S>`](crate::state::OnTransition) - which are used26//! to trigger systems specifically during matching transitions.27//! - A [`StateTransitionEvent<S>`](crate::state::StateTransitionEvent) that gets fired when a given state changes.28//! - The [`in_state<S>`](crate::condition::in_state) and [`state_changed<S>`](crate::condition::state_changed) run conditions - which are used29//! to determine whether a system should run based on the current state.30//!31//! Bevy also provides functionality for managing the lifetime of entities in the context of game states, using the [`state_scoped`] module.32//! Specifically, the marker components [`DespawnOnEnter<S>`](crate::state_scoped::DespawnOnEnter) and [`DespawnOnExit<S>`](crate::state_scoped::DespawnOnExit) are provided for despawning entities on state transition.33//! This, especially in combination with system scheduling, enables a flexible and expressive way to manage spawning and despawning entities.3435#![cfg_attr(36any(docsrs, docsrs_dep),37expect(38internal_features,39reason = "rustdoc_internals is needed for fake_variadic"40)41)]42#![cfg_attr(any(docsrs, docsrs_dep), feature(rustdoc_internals))]4344#[cfg(feature = "std")]45extern crate std;4647extern crate alloc;4849// Required to make proc macros work in bevy itself.50extern crate self as bevy_state;5152#[cfg(feature = "bevy_app")]53/// Provides [`App`](bevy_app::App) and [`SubApp`](bevy_app::SubApp) with state installation methods54pub mod app;55/// Provides extension methods for [`Commands`](bevy_ecs::prelude::Commands).56pub mod commands;57/// Provides definitions for the runtime conditions that interact with the state system58pub mod condition;59/// Provides definitions for the basic traits required by the state system60pub mod state;6162/// Provides tools for managing the lifetime of entities based on state transitions.63pub mod state_scoped;64#[cfg(feature = "bevy_app")]65/// Provides [`App`](bevy_app::App) and [`SubApp`](bevy_app::SubApp) with methods for registering66/// state-scoped events.67pub mod state_scoped_events;6869#[cfg(feature = "bevy_reflect")]70/// Provides definitions for the basic traits required by the state system71pub mod reflect;7273/// The state prelude.74///75/// This includes the most common types in this crate, re-exported for your convenience.76pub mod prelude {77#[cfg(feature = "bevy_app")]78#[doc(hidden)]79pub use crate::{app::AppExtStates, state_scoped_events::StateScopedMessagesAppExt};8081#[cfg(feature = "bevy_reflect")]82#[doc(hidden)]83pub use crate::reflect::{ReflectFreelyMutableState, ReflectState};8485#[doc(hidden)]86pub use crate::{87commands::CommandsStatesExt,88condition::*,89state::{90last_transition, ComputedStates, EnterSchedules, ExitSchedules, NextState, OnEnter,91OnExit, OnTransition, PreviousState, State, StateSet, StateTransition,92StateTransitionEvent, States, SubStates, TransitionSchedules,93},94state_scoped::{DespawnOnEnter, DespawnOnExit},95};96}9798#[cfg(test)]99mod tests {100use bevy_app::{App, PreStartup};101use bevy_ecs::{102resource::Resource,103system::{Commands, ResMut},104};105use bevy_state_macros::States;106107use crate::{108app::{AppExtStates, StatesPlugin},109state::OnEnter,110};111112#[test]113fn state_transition_runs_before_pre_startup() {114// This test is not really a "requirement" of states (we could run state transitions after115// PreStartup), but this is the current policy and it is useful to ensure we are following116// it if we ever change how we initialize stuff.117118let mut app = App::new();119app.add_plugins(StatesPlugin);120121#[derive(States, Default, PartialEq, Eq, Hash, Debug, Clone)]122enum TestState {123#[default]124A,125#[expect(126dead_code,127reason = "This struct is used as a compilation test to test the derive macros, and as such is intentionally never constructed."128)]129B,130}131132#[derive(Resource, Default, PartialEq, Eq, Debug)]133struct Thingy(usize);134135app.init_state::<TestState>();136137app.add_systems(OnEnter(TestState::A), move |mut commands: Commands| {138commands.init_resource::<Thingy>();139});140141app.add_systems(PreStartup, move |mut thingy: ResMut<Thingy>| {142// This system will fail if it runs before OnEnter.143thingy.0 += 1;144});145146app.update();147148// This assert only succeeds if first OnEnter(TestState::A) runs, followed by PreStartup.149assert_eq!(app.world().resource::<Thingy>(), &Thingy(1));150}151}152153154