25 releases
Uses new Rust 2024
| 0.17.2 | Oct 4, 2025 |
|---|---|
| 0.17.0 | Sep 30, 2025 |
| 0.16.1 | May 30, 2025 |
| 0.16.0-rc.3 | Mar 31, 2025 |
| 0.14.0 | Jul 4, 2024 |
#2757 in Game dev
136,655 downloads per month
Used in 318 crates
(23 directly)
5MB
88K
SLoC
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.
This module provides 3 distinct types of state, all of which implement the States trait:
- Standard
Statescan only be changed by manually setting theNextState<S>resource. These states are the baseline on which the other state types are built, and can be used on their own for many simple patterns. See the states example for a simple use case. SubStatesare children of other states - they can be changed manually usingNextState<S>, but are removed from theWorldif the source states aren't in the right state. See the sub_states example for a simple use case based on the derive macro, or read the trait docs for more complex scenarios.ComputedStatesare fully derived from other states - they provide acomputemethod that takes in the source states and returns their derived value. They are particularly useful for situations where a simplified view of the source states is necessary - such as having anInAMenucomputed state, derived from a source state that defines multiple distinct menus. See the computed state example to see usage samples for these states.
Most of the utilities around state involve running systems during transitions between states, or determining whether to run certain systems, though they can be used more directly as well. This makes it easier to transition between menus, add loading screens, pause games, and more.
Specifically, Bevy provides the following utilities:
- 3 Transition Schedules -
OnEnter<S>,OnExit<S>andOnTransition<S>- which are used to trigger systems specifically during matching transitions. - A
StateTransitionEvent<S>that gets fired when a given state changes. - The
in_state<S>andstate_changed<S>run conditions - which are used to determine whether a system should run based on the current state.
Bevy also provides ("state-scoped entities")crate::state_scoped functionality for managing the lifetime of entities in the context of game states.
This, especially in combination with system scheduling, enables a flexible and expressive way to manage spawning and despawning entities.
Dependencies
~6–12MB
~247K SLoC