Use hooks + immutable components pattern for state transitions
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 161
Description
## What problem does this solve or what need does it fill?
The current schedule-based approach to state transitions is messy.
It allows for system ordering ambiguities (#13947), and forces users to reason about a once-per-frame `StateTransition` schedule (which other crates may add more copies of).
State transitions hold important global invariants: we should use our new tools to make it easier (and more consistent) to reason about them.
## What solution would you like?
1. Remove the `StateTransition` schedule.
2. Make `State` immutable (blocked on #17485).
3. Remove `NextState`, and make users change states via commands.
4. Use hooks to call the `OnEnter`/`OnExit`/`OnTransition` schedules when the state changes.
5. Remove `.init_state` in favor of simply calling `.init_resource` on states. (Use a deprecation notice to make this easier).
## What alternative(s) have you considered?
#18562 discusses the possibility of adding another schedule before the initial state transition to allow for startup systems to run before states are entered.
I feel strongly that we shouldn't do this: it should be impossible to be in a state without its on-enter systems to be called (and similarly for on-exit). Instead, we should enforce this invariant as strongly as possible, and ensure that users have the tools needed to delay entering into states until any requisite setup is complete.
## Additional context
Note that this conflicts with the desire to run something *before* states are entered stated in #18562. However, users could simply initialize their starting states in a system during `Startup`, rather than calling `.init_state`.
Contributor guide
Assessment
This issue has not been assessed yet.