bevy_state/lib.rs
1#![no_std]
2
3//! 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.
4//!
5//! This module provides 3 distinct types of state, all of which implement the [`States`](state::States) trait:
6//!
7//! - Standard [`States`](state::States) can only be changed by manually setting the [`NextState<S>`](state::NextState) resource.
8//! These states are the baseline on which the other state types are built, and can be used on
9//! their own for many simple patterns. See the [states example](https://github.com/bevyengine/bevy/blob/latest/examples/state/states.rs)
10//! for a simple use case.
11//! - [`SubStates`](state::SubStates) are children of other states - they can be changed manually using [`NextState<S>`](state::NextState),
12//! 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)
13//! for a simple use case based on the derive macro, or read the trait docs for more complex scenarios.
14//! - [`ComputedStates`](state::ComputedStates) are fully derived from other states - they provide a [`compute`](state::ComputedStates::compute) method
15//! that takes in the source states and returns their derived value. They are particularly useful for situations
16//! where a simplified view of the source states is necessary - such as having an `InAMenu` computed state, derived
17//! 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)
18//! to see usage samples for these states.
19//!
20//! Most of the utilities around state involve running systems during transitions between states, or
21//! determining whether to run certain systems, though they can be used more directly as well. This
22//! makes it easier to transition between menus, add loading screens, pause games, and more.
23//!
24//! Specifically, Bevy provides the following utilities:
25//!
26//! - 3 Transition Schedules - [`OnEnter<S>`](crate::state::OnEnter), [`OnExit<S>`](crate::state::OnExit) and [`OnTransition<S>`](crate::state::OnTransition) - which are used
27//! to trigger systems specifically during matching transitions.
28//! - A [`StateTransitionEvent<S>`](crate::state::StateTransitionEvent) that gets fired when a given state changes.
29//! - The [`in_state<S>`](crate::condition::in_state) and [`state_changed<S>`](crate::condition::state_changed) run conditions - which are used
30//! to determine whether a system should run based on the current state.
31//!
32//! Bevy also provides functionality for managing the lifetime of entities in the context of game states, using the [`state_scoped`] module.
33//! 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.
34//! This, especially in combination with system scheduling, enables a flexible and expressive way to manage spawning and despawning entities.
35
36#![cfg_attr(
37 any(docsrs, docsrs_dep),
38 expect(
39 internal_features,
40 reason = "rustdoc_internals is needed for fake_variadic"
41 )
42)]
43#![cfg_attr(any(docsrs, docsrs_dep), feature(rustdoc_internals))]
44
45#[cfg(feature = "std")]
46extern crate std;
47
48extern crate alloc;
49
50// Required to make proc macros work in bevy itself.
51extern crate self as bevy_state;
52
53#[cfg(feature = "bevy_app")]
54/// Provides [`App`](bevy_app::App) and [`SubApp`](bevy_app::SubApp) with state installation methods
55pub mod app;
56/// Provides extension methods for [`Commands`](bevy_ecs::prelude::Commands).
57pub mod commands;
58/// Provides definitions for the runtime conditions that interact with the state system
59pub mod condition;
60/// Provides definitions for the basic traits required by the state system
61pub mod state;
62
63/// Provides tools for managing the lifetime of entities based on state transitions.
64pub mod state_scoped;
65#[cfg(feature = "bevy_app")]
66/// Provides [`App`](bevy_app::App) and [`SubApp`](bevy_app::SubApp) with methods for registering
67/// state-scoped events.
68pub mod state_scoped_events;
69
70#[cfg(feature = "bevy_reflect")]
71/// Provides definitions for the basic traits required by the state system
72pub mod reflect;
73
74/// The state prelude.
75///
76/// This includes the most common types in this crate, re-exported for your convenience.
77pub mod prelude {
78 #[cfg(feature = "bevy_app")]
79 #[doc(hidden)]
80 pub use crate::{app::AppExtStates, state_scoped_events::StateScopedMessagesAppExt};
81
82 #[cfg(feature = "bevy_reflect")]
83 #[doc(hidden)]
84 pub use crate::reflect::{ReflectFreelyMutableState, ReflectState};
85
86 #[doc(hidden)]
87 pub use crate::{
88 commands::CommandsStatesExt,
89 condition::*,
90 state::{
91 last_transition, ComputedStates, EnterSchedules, ExitSchedules, NextState, OnEnter,
92 OnExit, OnTransition, State, StateSet, StateTransition, StateTransitionEvent, States,
93 SubStates, TransitionSchedules,
94 },
95 state_scoped::{DespawnOnEnter, DespawnOnExit},
96 };
97}
98
99#[cfg(test)]
100mod tests {
101 use bevy_app::{App, PreStartup};
102 use bevy_ecs::{
103 resource::Resource,
104 system::{Commands, ResMut},
105 };
106 use bevy_state_macros::States;
107
108 use crate::{
109 app::{AppExtStates, StatesPlugin},
110 state::OnEnter,
111 };
112
113 #[test]
114 fn state_transition_runs_before_pre_startup() {
115 // This test is not really a "requirement" of states (we could run state transitions after
116 // PreStartup), but this is the current policy and it is useful to ensure we are following
117 // it if we ever change how we initialize stuff.
118
119 let mut app = App::new();
120 app.add_plugins(StatesPlugin);
121
122 #[derive(States, Default, PartialEq, Eq, Hash, Debug, Clone)]
123 enum TestState {
124 #[default]
125 A,
126 #[expect(
127 dead_code,
128 reason = "This struct is used as a compilation test to test the derive macros, and as such is intentionally never constructed."
129 )]
130 B,
131 }
132
133 #[derive(Resource, Default, PartialEq, Eq, Debug)]
134 struct Thingy(usize);
135
136 app.init_state::<TestState>();
137
138 app.add_systems(OnEnter(TestState::A), move |mut commands: Commands| {
139 commands.init_resource::<Thingy>();
140 });
141
142 app.add_systems(PreStartup, move |mut thingy: ResMut<Thingy>| {
143 // This system will fail if it runs before OnEnter.
144 thingy.0 += 1;
145 });
146
147 app.update();
148
149 // This assert only succeeds if first OnEnter(TestState::A) runs, followed by PreStartup.
150 assert_eq!(app.world().resource::<Thingy>(), &Thingy(1));
151 }
152}