States

Overview

In games sometimes you need to track the state of things. For Example:

  • The Direction you are traveling: North, South, East, West

  • The player’s health: 1-3

  • The action your player is performing: Idle, Walk, Run, Attack

  • Items in a chest: Food, Weapons, Money

  • Game State: InGame, InMenu, Victory, GameOver

  • Weapon Types: Stabbing, Blugeoning, Magical

This section covers several ways on how to manage game state logic.

State Example: Rock Paper Scissors

The examples in this section demonstrate the following behaviors:

  • There exists game objects in the scene with a state of Rock, Paper, and Scissors

  • When these game objects collide the loser is destroyed (rock collides with scissors -> scissors is destroyed)

The examples in this section also explore the following questions:

  • If we we add a new state to the scene (“Dynamite”) that can destroy Rock, Paper, Scissors and Dynamite, how difficult is it to implement it given the existing game structure?

Using Enum States

Tip

  • Enums are good for a fixed number of elements. If you anticipate adding additional elements in the future be prepared to make extensive edits to your code, especially if many scrips reference the enum.

Base Implementation

  • Elemental

    • Defines an enum Element with items “Rock”, “Paper”, and “Scissors”

    • Has a Element attribute

    • Has a method that detects if it can destroy another element

    • OnTriggerEnter destroys the other game object if the other contains an element it can destroy.

Adding Dynamite Implementation

  • Had to add a new enum to the Element enum.

  • Had to update the logic of CanDestoryElement. Had to add a switch case for Dynamite and had to account for the Dynamite under other cases.

Using Scriptable Object States

Base Implementation

  • Element

    • A Scriptable Object

    • Contains a list (CanDestory) of elements that it can destroy

  • Elemental

    • Has a Element attribute

    • OnTriggerEnter destroys the other game object if it contains an element that is present in the Element’s CanDestory List

  • In the Unity Editor

    1. Next create 3 scriptable object assets from Element and name them Rock, Paper, and Scissors

    2. Inside the Rock asset, we add Scissors to the CanDestory List. This repeats similarly for Paper and Scissors.

Adding Dynamite Implementation

  • We create a new scriptable object assets from Element and name it Dynamite

  • Inside the Dynamite asset, we add Rock, Paper, and Scissors to the CanDestory List.

Scriptable Object Variables

Lets say you have a scene with the name level_0 and you use the string level_0 in some of your scripts. Maybe at some point you change the level name or you want the level to be replaced by a scene with the name level_1. In this scenario you would have to track down each use of the level_0 string and replace it with level_1. Instead of repeating this process every time the scene name is altered, you could make a reference to a scriptable object storing the strings value. Now if the string needs to be altered all you would have to do is change the string inside the scriptable object.

Implementation

  • FloatVariable

    • A scriptable object that Contains the property value with the type float

  • FloatReference

    • Contains a property floatVariable of type FloatVariable as well as property of constantValue of type float

    • The user can opt to use the value of the constant or float variable by specifying the boolean property useConstant

    • scripts retrieve a float from FloatReference using the value property.

  • Other scriptable objects of different types can be built off of the same framework of the 2 above classes.

Example

Click to download ScriptableObjectVariablesExample.unitypackage.