state

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package state provides generic state machines whose transitions are driven by scheduled system updates.

Index

Constants

View Source
const SystemName = "state"

SystemName is the registered name for the state system.

Variables

View Source
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 New

func New[S comparable]() *Machine[S]

New returns a machine starting in S's zero state.

func (*Machine[S]) Current

func (m *Machine[S]) Current() S

Current returns the machine's current state.

func (*Machine[S]) Is

func (m *Machine[S]) Is(value S) bool

Is reports whether value is the 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

func (m *Machine[S]) Pending() (S, bool)

Pending returns the accepted destination waiting to be processed, if any.

func (*Machine[S]) Request

func (m *Machine[S]) Request(next S) error

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

func (m *Machine[S]) Restart() error

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

func (m *Machine[S]) WithInitial(initial S) *Machine[S]

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL