Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_state/src/lib.rs
6595 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")[`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::StateScopedEventsAppExt};
80
81
#[cfg(feature = "bevy_reflect")]
82
#[doc(hidden)]
83
pub use crate::reflect::{ReflectFreelyMutableState, ReflectState};
84
85
#[doc(hidden)]
86
pub use crate::{
87
commands::CommandsStatesExt,
88
condition::*,
89
state::{
90
last_transition, ComputedStates, EnterSchedules, ExitSchedules, NextState, OnEnter,
91
OnExit, OnTransition, State, StateSet, StateTransition, StateTransitionEvent, States,
92
SubStates, TransitionSchedules,
93
},
94
state_scoped::{DespawnOnEnter, DespawnOnExit},
95
};
96
}
97
98
#[cfg(test)]
99
mod tests {
100
use bevy_app::{App, PreStartup};
101
use bevy_ecs::{
102
resource::Resource,
103
system::{Commands, ResMut},
104
};
105
use bevy_state_macros::States;
106
107
use crate::{
108
app::{AppExtStates, StatesPlugin},
109
state::OnEnter,
110
};
111
112
#[test]
113
fn state_transition_runs_before_pre_startup() {
114
// This test is not really a "requirement" of states (we could run state transitions after
115
// PreStartup), but this is the current policy and it is useful to ensure we are following
116
// it if we ever change how we initialize stuff.
117
118
let mut app = App::new();
119
app.add_plugins(StatesPlugin);
120
121
#[derive(States, Default, PartialEq, Eq, Hash, Debug, Clone)]
122
enum TestState {
123
#[default]
124
A,
125
#[expect(
126
dead_code,
127
reason = "This struct is used as a compilation test to test the derive macros, and as such is intentionally never constructed."
128
)]
129
B,
130
}
131
132
#[derive(Resource, Default, PartialEq, Eq, Debug)]
133
struct Thingy(usize);
134
135
app.init_state::<TestState>();
136
137
app.add_systems(OnEnter(TestState::A), move |mut commands: Commands| {
138
commands.init_resource::<Thingy>();
139
});
140
141
app.add_systems(PreStartup, move |mut thingy: ResMut<Thingy>| {
142
// This system will fail if it runs before OnEnter.
143
thingy.0 += 1;
144
});
145
146
app.update();
147
148
// This assert only succeeds if first OnEnter(TestState::A) runs, followed by PreStartup.
149
assert_eq!(app.world().resource::<Thingy>(), &Thingy(1));
150
}
151
}
152
153