Day 102 — C# Fundamentals: Enums
Hey and welcome!
This was meant to be the first article on the new project but I came across the wonder of enums and I wanted to talk about them. We’ve come across data types in the past in the form of string, ints and bools and an enum is one of those as well. The data it stores are readable selections based on integer numbers and are typically written with PascalCasing.
Better to see what I mean in action though:
public class GraphicsSettings : MonoBehaviour
{
public enum GraphicsSelector
{
Low, // 0
Medium, // 1
High, // 2
Ultra // 3
}
}
This is how we go about defining an enum, pretty easy here as you just need to input each value as readable text separated by a comma making sure that the last value does not have one. The order you enter in your values also determines the value of the int value that’s based on it which I’ve added as a comment next to each one.
You can also manually set the int values by doing something like this:
public class GraphicsSettings : MonoBehaviour
{
public enum GraphicsSelector
{
Low = 3,
Medium = 10,
High = 1,
Ultra = 0
}
}
The amount of places that can come in handy though is a bit limited but it’s still handy to know! With the enum as it is now we can’t actually do anything with it currently since they can’t do anything by themselves. If you checked in the inspector you wouldn’t even see it in there.
Let’s go ahead and add in this line so that we can make an instance of it that we can use:
public GraphicsSelector currentSetting;
Now if you check the inspector for this script component you’ll see a dropdown list where you can select the different values you’ve defined:
I don’t know about you but that’s pretty cool! It’s a lot more readable than numbers and is handy for any designers working on the game.
Lastly let’s cap this off by seeing how this can be made use of in code.
private void Start()
{
switch (currentSetting)
{
case GraphicsSelector.Low:
Debug.Log("Low settings selected!");
break;
case GraphicsSelector.Medium:
Debug.Log("Medium settings selected!");
break;
case GraphicsSelector.High:
Debug.Log("High settings selected!");
break;
case GraphicsSelector.Ultra:
Debug.Log("Ultra settings selected!");
break;
default:
break;
}
}
Switch statements are our friend here when working with Enums. One thing you’ll notice is that we’re doing case Graphics.Selector.Low and case GraphicsSelector.Medium instead of case 0 and case 1 that we’re normally used to with switch statements. That’s because these aren’t integers that we’re working with so we can’t directly reference the int value they’re based on without type casting the enum into an int beforehand.
More importantly for this switch statement though is when a particular value has been selected by the player or designer it will run the relevant code here. So if Low is chosen then it will log “Low settings selected!” to the console and so on. In a full game you’d have the code or a reference to the script that handles changing up the graphics settings here.
Type Casting
To finish up here let’s talk about what type casting is and how it can be useful when working with enums. Type casting is when you assign the value of one data type to another type entirely, so in the example coming up we’re going to cast an enum into an int.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class SelectLevel : MonoBehaviour
{
public enum LevelSelector
{
Artisans,
Peacekeepers,
MagicCrafters,
BeastMakers,
DreamWeavers
} public LevelSelector chosenLevel; private void Start()
{
SceneManager.LoadScene((int)chosenLevel);
}
}
In the above code we’re looking to load a level depending on the player’s selection. When working with scene management you can either enter the scene name as a string or pass in the int value of the scene when using LoadScene. Since we’re working with enums we won’t be able to pass in just chosenLevel by itself similar to the problem we came across with the switch statement.
By typing it out as (int)chosenLevel this is what’s needed in order to type cast our enum into an int. Our enums are already based on integer values so the number that’s returned in here will be based on the selected value’s order in the enum e.g. if MagicCrafters is chosen that would return 2 etc.
This is also where manually assigning the int by doing MagicCrafters = 8 would be handy as your levels may not be nice and ordered in your build settings or you may have a main menu or different scenes that come before it and so on.
I think that’s everything useful to talk about enums for the time being though, next time is the start of a new project!