how to make class type safe
using System;
using System.Reflection;
using UnityEngine;
public class StateMachine<TStates> where TStates : Enum
{
private TStates initialState;
private TStates currentState;
private MonoBehaviour component;
public StateMachine(MonoBehaviour component)
{
this.component = component;
}
public StateMachine(MonoBehaviour component, TStates initialState)
{
this.component = component;
this.initialState = initialState;
SetState(this.initialState);
}
public void SetState(TStates newState)
{
Type componentType = component.GetType();
if (currentState != null)
{
if (currentState.Equals(newState))
return;
componentType.GetMethod("On" + currentState + "Exit", BindingFlags.NonPublic | BindingFlags.Instance)?.Invoke(component, null);
}
else
initialState = newState;
currentState = newState;
componentType.GetMethod("On" + currentState + "Enter", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(component, null);
}
public void Update()
{
Type componentType = component.GetType();
if (currentState != null)
componentType.GetMethod("On" + currentState + "Update", BindingFlags.NonPublic | BindingFlags.Instance)?.Invoke(component, null);
}
public void Reset()
{
if (initialState != null)
SetState(initialState);
}
}
I have a finite state machine that I tried making myself and it works fine. However, people told me that it isn't type safe.
They told me to use switch statements but I don't see how I'm able to implement them.
Any way I can make it type safe?
public interface IStateHandle<TStates> where TStates : Enum
{
void OnEnter(TStates state);
void OnUpdate(TStates state);
void OnExit(TStates state);
}
public class StateMachine<TStates> where TStates : Enum
{
IStateHandle<TStates> _handle;
TStates _currentState;
public StateMachine(IStateHandle<TStates> handle)
{
_handle = handle;
}
public void Update()
{
_handle.OnUpdate(_currentState);
}
}