Finite State machines are frequently used in video games. They are easy to use (less than one hour to learn) and easy to implement (less than one hour to implement intermediate level of code.) The following system follows many aspects of SKED (Snapper Kadden, 1973). The system to be described below is a state notation compiler and fsm engine written in Unity. It is a Mealy machine when changes in the environment occur during the transition from one state to another. It has 10 parallel state sets with a maximum of 10 states per state set. Each state can have a maximum of 10 transitions per state. Currently the system is implemented for a desktop environment in Unity. It utilizes keyboard input (A - O, Q - S, U – Y with P T Z reserved for other uses. P is reserved to indicate probability (in increments of .1%), T indicates 1/100 of a second and Z (from 0 to 99) pulses communicate between parallel state sets and Unity gameObjects.
Calls to functions are implemented via Broadcast messages. Current functions implemented are:
Counter to increment counters with an index of 0 to 99
display Counter to display non-zero values of counters
sendZ to receive Z-pulses from Unity gameObjects.
And any designer created function.
Enclosed is a simple example. It consists on an empty game object (fsmParentObject) that contains the C# fsm script. There are 2 cubes that are children of fsmParentObjects (objects subjected to the finite state machine must be children.) Each cube has script (moveCube) that translates a cube based upon the value of a string variable. In addition each cube communicates with the finite state machine via Z pulses.
The finite state machine is represented in a file labeled finitestate.txt. This is a Notepad generated state file. The file contains:
//First One
PSS0
S0:
1D:Z3;counter,6->S0
PSS1
S0:
1A:changeDecide,Y;counter,2->S1
1C:displayCounter,1->S3
S1:
1B:changeDecide,N;counter,3-> S0
T1000:counter,4;changeDecide,N;->S2
S2:
P333:counter,5;displayCounter,1->S3
S3:
Z3:z5->S0
PSS2
S0:
Z3:counter,7->S0
PSS3
S0:
Z11:counter,11->S0
Z12:counter,12->S0
PSS indicates which parallel state set (from 0 to 9).
S0: indicates the state in that parallel state set.
1D:Z3;counter,6->S0 indicates that when the player presses the D key once, a Z3 pulse will be issued and counter index 6 will be incremented. In addition a transition will occur to state 0. Input is separated with a colon. Multiple function calls are separated by semi-colons. “->” indicates a transition from one state to another.
Needed addition: input from input manager.
Please try it out and let me know what you think.
SNAPPER, A. G., KADDEN, R. M. Time-sharing in a small computer based on a behavior notation system. In B. Weiss (Ed.), Digital computers in the behavior laboratory. New York: Appleton-Century-Crofts, 1973.
For additional citations see fsm.cs.
718358–26055–$FS.zip (184 KB)