Forum Thread
  Posts  
Examples of state machines used in Unity (Forums : Coding & Scripting : Examples of state machines used in Unity) Locked
Thread Options
Feb 26 2018 Anchor

Hello! I'm looking for some examples of state machines used in Unity to take care of game states. In fact, what I'd really like to find is somewhere I can get a complete game, even if I have to pay for the source code. Just a full game project that I can dissect to see how everything works together. I'm working on a game with a friend and we plan to use a state machine, but we aren't entirely certain we are going about it the right way. Thanks for your help.

iqew
iqew VFX Artist
Mar 10 2018 Anchor

There is nothing special about state machines in Unity. You can just look up any project in general and see how they did it. Not sure what exactly you're looking for here. You simply create an enum with different states you need and some kind of controller, which switches through them depending on your actions.

public enum GameState {MAIN_MENU, OPTIONS, IN_GAME}

public class Controller {

	public Button _optionsButton;
	public Button _startButton;

	private GameState _state = GameState.MAIN_MENU;

	void Awake() {

		_optionsButton.onClick.AddListener (() => ShowOptions());
		_startButton.onClick.AddListener (() => StartGame());
	}

	private void ShowOptions() {
		_state = GameState.OPTIONS;
		// Switch UI to options menu
	}

	private void StartGame() {
		_state = GameState.IN_GAME;
		// Start the game
	}
}

Just as an example. Whether your Controller is a MonoBehaviour on a GameObject in the scene or not is depending on your overall structure of the project.

Edited by: iqew

--

Need some polishing for your game with beautiful VFX? Check out my website: Game-vfx.com

Mar 10 2018 Anchor

Hello,

I made a small example for you: MEGA

I hope you will find it useful, have a nice day!

Dragonlord
Dragonlord Linux-Dragon of quick wit and sharp tongue
Mar 11 2018 Anchor

Actually any game in existence uses a state machine in one way or the other. State machine is a concept more than an implementation.

Using enums is one way to do a state machine (I call this "state variable based" state machine). Other possibilities are using classes based on a state interface (I call this "class based" state machine). Last but not least you can use state machine definition tools which work with some kind of state machine language (I call this "definition based" state machine). And of course you can mix and match this (I use both "class based" and "state variable based" mixed together for various kinds of state machines).

Basically you just want your scripts to store the current state (a variable containing an enumeration value or a pointer containing a class instance) and use code which alters behaviour depending on this state:

In pseudo-code this boils down to this here:

for state based state machines:

switch(state){
case STATE_ENUM_1:
   // do this
case STATE_ENUM_2:
   // do that
...
}

for class based state machines:

interface StateMachine{
func UserAction1()
func UserAction2()
...
}

class State1 implements StateMachine{
func UserAction1(){
   // do this
}
...
}

class State2 implements StateMachine{
func UserAction1(){
   // do that
}
...
}

If you see the concept the rest is just doing it the way your engine of choice allows to.

Edited by: Dragonlord

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.