pub trait States: 'static + Send + Sync + Clone + PartialEq + Eq + Hash + Debug {
const DEPENDENCY_DEPTH: usize = 1usize;
}
Expand description
Types that can define world-wide states in a finite-state machine.
The Default
trait defines the starting state.
Multiple states can be defined for the same world,
allowing you to classify the state of the world across orthogonal dimensions.
You can access the current state of type T
with the State<T>
resource,
and the queued state with the NextState<T>
resource.
State transitions typically occur in the OnEnter<T::Variant>
and OnExit<T::Variant>
schedules,
which can be run by triggering the StateTransition
schedule.
Types used as ComputedStates
do not need to and should not derive States
.
ComputedStates
should not be manually mutated: functionality provided
by the States
derive and the associated FreelyMutableState
trait.
§Example
use bevy_state::prelude::*;
use bevy_ecs::prelude::IntoSystemConfigs;
use bevy_ecs::system::ResMut;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
enum GameState {
#[default]
MainMenu,
SettingsMenu,
InGame,
}
fn handle_escape_pressed(mut next_state: ResMut<NextState<GameState>>) {
if escape_pressed {
next_state.set(GameState::SettingsMenu);
}
}
fn open_settings_menu() {
// Show the settings menu...
}
app.add_systems(Update, handle_escape_pressed.run_if(in_state(GameState::MainMenu)));
app.add_systems(OnEnter(GameState::SettingsMenu), open_settings_menu);
Provided Associated Constants§
sourceconst DEPENDENCY_DEPTH: usize = 1usize
const DEPENDENCY_DEPTH: usize = 1usize
How many other states this state depends on.
Used to help order transitions and de-duplicate ComputedStates
, as well as prevent cyclical
ComputedState
dependencies.