Documentation
¶
Overview ¶
Package state provides generic state machines whose transitions are driven by scheduled system updates.
Index ¶
- Constants
- Variables
- func NewSystem[S comparable](machine *Machine[S]) core.SystemFunc
- type Changed
- type Initialized
- type Machine
- func (m *Machine[S]) Current() S
- func (m *Machine[S]) Is(value S) bool
- func (m *Machine[S]) OnEnter(value S, hook core.SystemFunc)
- func (m *Machine[S]) OnExit(value S, hook core.SystemFunc)
- func (m *Machine[S]) Pending() (S, bool)
- func (m *Machine[S]) Request(next S) error
- func (m *Machine[S]) Restart() error
- func (m *Machine[S]) WithInitial(initial S) *Machine[S]
Constants ¶
const SystemName = "state"
SystemName is the registered name for the state system.
Variables ¶
var ErrTransitionPending = errors.New("state transition already pending")
ErrTransitionPending reports that a machine already has a different transition request waiting to be processed.
Functions ¶
func NewSystem ¶
func NewSystem[S comparable](machine *Machine[S]) core.SystemFunc
NewSystem returns a core.SystemFunc that processes a Machine's initial entry and queued transitions.
Register it in core.PhasePreStartup and core.PhasePreFrame to run initial entry before startup scene operations and transitions before frame scene operations. The first update runs the initial state's enter hooks; later updates apply pending transitions or restarts. Requests accepted from hooks are deferred until the next update.
The system resolves and caches the world's signal.Hub before processing the machine. Resolution errors leave the machine unchanged. It emits Initialized after successful initial entry and Changed after exit hooks succeed, before enter hooks run. In the engine, these signals expire before fixed ticks.
Hook errors stop the remaining hooks for that run and are returned from Update. An exit-hook error consumes the pending request without changing the current state. An enter-hook error during a transition is returned after Changed is emitted and the state has changed. Failed hooks are not retried, and their side effects are not rolled back. A failed initial entry does not emit Initialized. A nil machine returns an error from Update.
Types ¶
type Changed ¶
type Changed[S comparable] struct { // From is the state the machine left. From S // To is the machine's new current state. To S }
Changed is a signal emitted when a transition or restart is applied. It is emitted after exit hooks succeed and before enter hooks run. This means that a Changed event guarantees that the state has changed but not that enter hooks have run successfully.
type Initialized ¶
type Initialized[S comparable] struct { // State is the initial state whose enter hooks completed. State S }
Initialized is a signal emitted the first time a machine's initial enter hooks have run successfully. It contains the initial state whose enter hooks completed.
type Machine ¶
type Machine[S comparable] struct { // contains filtered or unexported fields }
Machine tracks a comparable state value and queues requested transitions until its system processes them. Its zero value starts in S's zero state. Access a machine only from the game loop's goroutine.
func (*Machine[S]) Current ¶
func (m *Machine[S]) Current() S
Current returns the machine's current state.
func (*Machine[S]) OnEnter ¶
func (m *Machine[S]) OnEnter(value S, hook core.SystemFunc)
OnEnter registers a core.SystemFunc that runs when value is entered, including the initial state during startup. An error stops the remaining hooks and is returned by the state system. A nil hook returns an error when run.
func (*Machine[S]) OnExit ¶
func (m *Machine[S]) OnExit(value S, hook core.SystemFunc)
OnExit registers a core.SystemFunc that runs before leaving value. An error stops the transition and is returned by the state system. A nil hook returns an error when run.
func (*Machine[S]) Pending ¶
Pending returns the accepted destination waiting to be processed, if any.
func (*Machine[S]) Request ¶
Request accepts next as the pending destination without running hooks. Requests for the current state are no-ops; an identical pending request coalesces, while a different pending destination returns ErrTransitionPending. A successful request means it was accepted, not that the transition completed.
func (*Machine[S]) Restart ¶
Restart accepts a request to exit and re-enter the current state on the next update. Repeated restart requests coalesce. If another destination is already pending, Restart returns an error wrapping ErrTransitionPending. A successful request means it was accepted, not that the restart completed.
func (*Machine[S]) WithInitial ¶
WithInitial returns a new machine starting in initial, with a copy of m's registered hooks. It leaves m unchanged and carries over neither pending requests nor initialization progress.