Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_state/src/lib.rs
6849 views
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 ("state-scoped entities") [`state_scoped`](`crate::state_scoped`) functionality for managing the lifetime of entities in the context of game states.
33
//! This, especially in combination with system scheduling, enables a flexible and expressive way to manage spawning and despawning entities.
34
35
#![cfg_attr(
36
any(docsrs, docsrs_dep),
37
expect(
38
internal_features,
39
reason = "rustdoc_internals is needed for fake_variadic"
40
)
41
)]
42
#![cfg_attr(any(docsrs, docsrs_dep), feature(rustdoc_internals))]
43
44
#[cfg(feature = "std")]
45
extern crate std;
46
47
extern crate alloc;
48
49
// Required to make proc macros work in bevy itself.
50
extern crate self as bevy_state;
51
52
#[cfg(feature = "bevy_app")]
53
/// Provides [`App`](bevy_app::App) and [`SubApp`](bevy_app::SubApp) with state installation methods
54
pub mod app;
55
/// Provides extension methods for [`Commands`](bevy_ecs::prelude::Commands).
56
pub mod commands;
57
/// Provides definitions for the runtime conditions that interact with the state system
58
pub mod condition;
59
/// Provides definitions for the basic traits required by the state system
60
pub mod state;
61
62
/// Provides tools for managing the lifetime of entities based on state transitions.
63
pub mod state_scoped;
64
#[cfg(feature = "bevy_app")]
65
/// Provides [`App`](bevy_app::App) and [`SubApp`](bevy_app::SubApp) with methods for registering
66
/// state-scoped events.
67
pub mod state_scoped_events;
68
69
#[cfg(feature = "bevy_reflect")]
70
/// Provides definitions for the basic traits required by the state system
71
pub mod reflect;
72
73
/// The state prelude.
74
///
75
/// This includes the most common types in this crate, re-exported for your convenience.
76
pub mod prelude {
77
#[cfg(feature = "bevy_app")]
78
#[doc(hidden)]
79
pub use crate::{app::AppExtStates, state_scoped_events::StateScopedMessagesAppExt};
80
81
#[cfg(feature = "bevy_reflect")]
82
#[doc(hidden)]
83
pub use crate::reflect::{ReflectFreelyMutableState, ReflectState};
84
85
#[doc(hidden)]
86
#[expect(
87
deprecated,
88
reason = "Temporarily re-exporting deprecated type for transition"
89
)]
90
pub use crate::state_scoped::StateScoped;
91
92
#[doc(hidden)]
93
pub use crate::{
94
commands::CommandsStatesExt,
95
condition::*,
96
state::{
97
last_transition, ComputedStates, EnterSchedules, ExitSchedules, NextState, OnEnter,
98
OnExit, OnTransition, State, StateSet, StateTransition, StateTransitionEvent, States,
99
SubStates, TransitionSchedules,
100
},
101
state_scoped::{DespawnOnEnter, DespawnOnExit},
102
};
103
}
104
105
#[cfg(test)]
106
mod tests {
107
use bevy_app::{App, PreStartup};
108
use bevy_ecs::{
109
resource::Resource,
110
system::{Commands, ResMut},
111
};
112
use bevy_state_macros::States;
113
114
use crate::{
115
app::{AppExtStates, StatesPlugin},
116
state::OnEnter,
117
};
118
119
#[test]
120
fn state_transition_runs_before_pre_startup() {
121
// This test is not really a "requirement" of states (we could run state transitions after
122
// PreStartup), but this is the current policy and it is useful to ensure we are following
123
// it if we ever change how we initialize stuff.
124
125
let mut app = App::new();
126
app.add_plugins(StatesPlugin);
127
128
#[derive(States, Default, PartialEq, Eq, Hash, Debug, Clone)]
129
enum TestState {
130
#[default]
131
A,
132
#[expect(
133
dead_code,
134
reason = "This struct is used as a compilation test to test the derive macros, and as such is intentionally never constructed."
135
)]
136
B,
137
}
138
139
#[derive(Resource, Default, PartialEq, Eq, Debug)]
140
struct Thingy(usize);
141
142
app.init_state::<TestState>();
143
144
app.add_systems(OnEnter(TestState::A), move |mut commands: Commands| {
145
commands.init_resource::<Thingy>();
146
});
147
148
app.add_systems(PreStartup, move |mut thingy: ResMut<Thingy>| {
149
// This system will fail if it runs before OnEnter.
150
thingy.0 += 1;
151
});
152
153
app.update();
154
155
// This assert only succeeds if first OnEnter(TestState::A) runs, followed by PreStartup.
156
assert_eq!(app.world().resource::<Thingy>(), &Thingy(1));
157
}
158
}
159
160