pub trait States: 'static + Send + Sync + Clone + PartialEq<Self> + Eq + Hash + Debug + Default {
type Iter: Iterator<Item = Self>;
// Required method
fn variants() -> Self::Iter;
}
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 via the apply_state_transition::<T>
system.
Systems that run each frame in various states are typically stored in the main schedule,
and are conventionally part of the [OnUpdate(T::Variant)
] system set.
Example
use bevy_ecs::prelude::States;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
enum GameState {
#[default]
MainMenu,
SettingsMenu,
InGame,
}