Skip to content

Use hooks + immutable components pattern for state transitions #18583

Open
@alice-i-cecile

Description

@alice-i-cecile

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<T> immutable (blocked on Store resources as components on singleton entities #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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possibleS-BlockedThis cannot move forward until something else changesX-ControversialThere is active debate or serious implications around merging this PR

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions