Unity C# State Management

Unity 3D & C#

Unity3D is a powerful suite of tools (Project IDE, Code IDE, run-time) for game development.

As always, RivelloMultimediaConsulting.com/unity/ will be the central location for deep articles and tutorials, Facebook.com/RivelloMultimediaConsulting (like us!) will engage the growing RMC+Unity community, and for the latest opinions and cool links follow me at Twitter.com/srivello.

There is incredible momentum in the Unity3D product and its community. I recently posted “The Best C# Code Naming Conventions” and “Unity Best Practices“.  I came across one best-practice that requires a bit of setup, so here it is; using symlinks for game development.

What Is State?

As UnityGems points out – Every program you write is a state machine.  The moment you write an if statement you have created code that can be in at least one of two states – the more code you write the more states the program can be in.  An explosion of switch and if statements can quickly get out of hand, with strange bugs appearing for apparently impossible to fathom reasons as the complexity of the solution increases and interactions between components causes unexpected consequences.  Your project becomes hard to extend and even harder to conceptualize.

How Do We Manage It?

You are probably already managing state in your projects; perhaps just with simple conditionals. As your skills mature to match your growing needs you may benefit from some alternative solutions.

Here is a partial list of options (from least to most complex/powerful);

  1. Conditionals (or similar ‘manual’ technique)*
  2. State Pattern
  3. State Machine*

* Full source code and Unity project available. See ‘Member Resources’ below.

State Machine Example

In the following example, imagine a camera and a cube in the scene. The cube has one script attached; StatesByStatePatternComponent. This class holds _stateType as a variable of type StateType. Upon user mouse-click the state is toggled. Toggling updates the iState variable of type IState to either an instance of the class FirstState or the class SecondState. The granular, state-specific functionality is handled within those two classes.

In a more complex use of the State Pattern, such as that of a full game, the state could be used to handle the input and movement. Perhaps a PlayerComponent which controls our hero has a WalkingState, JumpingState, and ShootingState. We would want only ONE of these to be active at a time and the character would behave differently depending on the state. Just like that!

3 Examples are below

  • States By Conditionals– This is the most simple, intuitive approach for beginners with simple needs
  • States By State Pattern – This is more robust. This addresses the typical demands of many projects. For advanced uses, add concepts of actions, triggers, transitions.
  • States By Interfaces – An alternative approach.

 

States By Conditionals

States By State Pattern

States By Interfaces

 


Wait, An FSM?

Finite State Machines (FSM) are as much a style of programming as they are the code that realises them – says UnityGems.  We strongly urge you to consider thinking about your game and everything in it in terms of the states that they can maintain.
What are Finite State Machines?

Want to see my full demo of FSM? It is not yet posted. Please comment below.

The clue is in the word finite – in other words: limited, known and understood.  When you build a finite state machine you define clearly all of the states that an object can occupy and you think about the object being in just one of those states at a time.

Thinking about your project in terms of finite state machines helps you conceptualize your game at a higher level, enabling you to keep more of it in your head at one time and more clearly see the path to implementing the killer feature you just thought up or your play testers demanded.

What Do YOU Think?

Have a comment? Please post below. This is a hot topic and I’ll respond promptly over the next month.

Member Resources

[private_Free member]Enjoy this members-only content!

[/private_Free member]