Show / Hide Table of Contents

State Machine

We use State Machines pretty heavily in our scenes. The bulk of this is done with two DimSums, StateMachine and MachineState. These DimSums allow you to build a state machine in the Unity inspector, trigger the machine to go to different states and to have Unity Events fired when a state is entered or exited. Here we have an example of a Game Object that has the State Machine script on it:

App Icon

This is what the State Machine Script looks like in the inspector:

App Icon

The Machine State script is used to define each state in the State Machine. These are examples of Game Objects with the Machine State script on them under the parent Game Object that has the State Machine:

App Icon

Example of the events that fire on the entering and exiting of a Machine State:

App Icon

Transitioning Between States

A transition is acheived by calling StateMachine.Trigger and passing a string that is the trigger name. A MachineState can specify 2 types of trigger consumption rules:

  1. an AnyState that specifies a trigger that will bring you into the state from any other state (i.e. an unconditional transition - the trigger will always be used)
  2. a Transition that specifies a trigger that will take you out of a specific state and into a specific state (i.e. a conditional transition - the trigger will only be used if the machine is currently in a state that can use the trigger)

In the example the Any State name is "Say". If StateMachine.Trigger is called with "Say" the machine will go to the shown state, regardless of the state the machine was in before. In the example if StateMachine.Trigger is called with "RecordingFinished" AND the machine is in the shown state, the machine will transition to the SayTargetSlow state.

Back to top Generated by DocFX