Module utils.state_machine

This module provides a classical mealy/moore state machine.

Each machine in constructed by calling new() States and Transitions are lazily added to the machine as transition handlers and state tick handlers are registered. However the state machine must be fully defined after init is done. Dynamic machine changes are currently unsupported An example usage can be found here: map_gen\combined\tetris\control.lua

Functions

Module.transition (self, new_state) Transitions the supplied machine into a given state and executes all transaction_callbacks
Module.in_state (self, state) Is this machine in this state?
Module.machine_tick (self) Invoke a machine tick.
Module.register_state_tick_callback (self, state, callback) Register a handler that will be invoked by StateMachine.machine_tick You may register multiple handlers for the same transition NOTICE: This function will invoke an error if called after init.
Module.register_transition_callback (self, state, state, callback) Register a handler that will be invoked by StateMachine.transition You may register multiple handlers for the same transition NOTICE: This function will invoke an error if called after init.
Module.new (init_state) Constructs a new state machine


Functions

Module.transition (self, new_state)
Transitions the supplied machine into a given state and executes all transaction_callbacks

Parameters:

  • self StateMachine
  • new_state number/string The new state to transition to
Module.in_state (self, state)
Is this machine in this state?

Parameters:

  • self StateMachine
  • state number/string

Returns:

    boolean
Module.machine_tick (self)
Invoke a machine tick. Will execute all in_state_callbacks of the given machine

Parameters:

  • self StateMachine the machine, whose handlers will be invoked
Module.register_state_tick_callback (self, state, callback)
Register a handler that will be invoked by StateMachine.machine_tick You may register multiple handlers for the same transition NOTICE: This function will invoke an error if called after init. Dynamic machine changes are currently unsupported

Parameters:

  • self StateMachine the machine
  • state number/string The state, that the machine will be in, when callback is invoked
  • callback function
Module.register_transition_callback (self, state, state, callback)
Register a handler that will be invoked by StateMachine.transition You may register multiple handlers for the same transition NOTICE: This function will invoke an error if called after init. Dynamic machine changes are currently unsupported

Parameters:

  • self StateMachine the machine
  • state number/string entering state
  • state number/string entering state
  • callback function
Module.new (init_state)
Constructs a new state machine

Parameters:

  • init_state number/string The starting state of the machine

Returns:

    StateMachine The constructed state machine object
generated by LDoc 1.4.3 Last updated 2019-05-29 22:30:49