Skip to main content

Module state

Module state 

Source
Expand description

Typed game states with enter/exit/transition schedules.

A state set is any small value type (usually an enum) that is Eq + Hash; one State<S> resource holds the current value and one NextState<S> holds a pending change. Requesting a transition from any system is a write to NextState<S>; the sim applies it once per fixed step, before the step’s main schedule, so a transition never interleaves with gameplay.

Apply order for A -> B matches the reference (bevy_state):

  1. OnExit(A) - the State<S> resource still reads A
  2. State<S> = B
  3. OnTransition { exited: A, entered: B }
  4. OnEnter(B)

If an enter/exit system requests another transition, it is resolved on the same step, up to MAX_CHAIN links; beyond that the chain is dropped and the last state stands. That bound stops a self-triggering transition from spinning the step forever.

Determinism: transitions run inside the fixed step and in registration order, so a replay that requests the same transitions at the same ticks resolves them identically. Do not request transitions from a thread.

Structs§

NextState
A pending transition. Writing here requests a change; the sim applies it before the next step’s main schedule.
State
The current value of a state set.

Constants§

MAX_CHAIN
Most transition links a single step resolves before giving up.

Traits§

States
A state set: a plain value used as a game state.

Functions§

in_state
Run condition: only run the configured systems while the state is state.