Lightweight State Machine for Unity for global states such as Win or Lose.
- Easy to use
- Supports custom game states
- Awesome performance
- Install files into your Unity Project
- Add
GlobalStateMachineEntryon anyGameObjectin scene
-
Write using
using NTC.GlobalStateMachine -
Inherit script from
StateMachineUserand override the methods you need
public class Sample : StateMachineUser
{
protected override void OnAwake()
{
Debug.Log("On Awake");
}
protected override void OnDestroyOverridable()
{
Debug.Log("On Destroy");
}
protected override void OnGameWin()
{
Debug.Log("You Won");
}
protected override void OnGameLose()
{
Debug.Log("You Lose");
}
protected override void OnGameFinish()
{
Debug.Log("You Finished");
}
protected override void OnGamePause()
{
Debug.Log("Game Paused");
}
}- Push state you need
GlobalStateMachine.Push(new WinState());GlobalStateMachine.Push<WinState>();GlobalStateMachine.Push(GameStates.Win);You can push any state if entity is in the trigger
public class PushWinStateOnPlayerEnter : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent(out PlayerUnit playerUnit))
{
GlobalStateMachine.Push(new WinState());
}
}
}Also you can get a new state by extension method GetState() of the GameStates enum
public class PushStateOnPlayerEnter : MonoBehaviour
{
[SerializeField] private GameStates stateToPush = GameStates.Win;
private void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent(out PlayerUnit playerUnit))
{
GlobalStateMachine.Push(stateToPush);
}
}
}For example, you can play audio on game win
public class PlayAudioOnWin : StateMachineUser
{
[SerializeField] private AudioSource audioSource;
[SerializeField] private AudioClip audioClip;
protected override void OnGameWin()
{
audioSource.PlayOneShot(audioClip);
}
}You can check any state for pushed
var isStatePushed = GlobalStateMachine.WasPushed<TState>();- Create a new
classand inherit one fromGameState
public sealed class CustomState : GameState { }- Optionally you can override the
CanRepeatparameter (default value istrue)
public sealed class CustomState : GameState
{
public override bool CanRepeat => false;
}- Optionally you can block any next states for the
CustomState
public sealed class CustomState : GameState
{
public override bool CanRepeat => false;
protected override void BlockNextStates()
{
BlockNextState<RunningState>();
BlockNextState<LoseState>();
}
}- Open
StateMachineUserand write a virtual method for new State
protected virtual void OnCustomState() { }- Bind callback for new state in method
BindCallbacks()
this.On<CustomState>(OnCustomState);- What should be the end result:
public abstract class StateMachineUser : MonoBehaviour
{
private void Awake()
{
BindCallbacks();
OnAwake();
}
private void OnDestroy()
{
this.RemoveSubscriber();
OnDestroyOverridable();
}
private void BindCallbacks()
{
this.On<RunningState>(OnGameRun);
this.On<WinState>(OnGameWin);
this.On<LoseState>(OnGameLose);
this.On<PausedState>(OnGamePause);
this.On<WinState, LoseState>(OnGameFinish);
this.On<CustomState>(OnCustomState); // <<< Bind Callback For The New Custom State
}
protected virtual void OnAwake() { }
protected virtual void OnDestroyOverridable() { }
protected virtual void OnGameRun() { }
protected virtual void OnGameWin() { }
protected virtual void OnGameLose() { }
protected virtual void OnGamePause() { }
protected virtual void OnGameFinish() { }
protected virtual void OnCustomState() { } // <<< Virtual Method For The New Custom State
}