Skip to content

Instantly share code, notes, and snippets.

@ikriz
Forked from AngryAnt/MyStateMachine.cs
Created October 24, 2011 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikriz/1308675 to your computer and use it in GitHub Desktop.
Save ikriz/1308675 to your computer and use it in GitHub Desktop.
A simple state machine using a dictionary of delegates indexed by an enum.
using UnityEngine;
using System.Collections.Generic;
public class MyStateMachine : MonoBehaviour
{
public delegate void StateHandlerDelegate ();
public enum MyStateType
{
Idle,
Combat,
Juggling
}
private Dictionary<MyStateType, StateHandlerDelegate> m_StateHandlers;
private MyStateType m_State;
void Start ()
{
m_StateHandlers = new Dictionary<MyStateType, StateHandlerDelegate> ();
m_StateHandlers[MyStateType.Idle] = IdleHandler;
m_StateHandlers[MyStateType.Combat] = CombatHandler;
m_StateHandlers[MyStateType.Juggling] = JugglingHandler;
}
void Update ()
{
m_StateHandlers[m_State] ();
}
void IdleHandler ()
{
// Handle the state and set m_State to change to another state
}
void CombatHandler ()
{
// Handle the state and set m_State to change to another state
}
void JugglingHandler ()
{
// Handle the state and set m_State to change to another state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment