Skip to content

Instantly share code, notes, and snippets.

@AngryAnt
Created May 31, 2011 08:45
Show Gist options
  • Save AngryAnt/1000180 to your computer and use it in GitHub Desktop.
Save AngryAnt/1000180 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