For all of my (rather short) time programming I have never quite understood the purpose of enumeration type nor how to properly use it.
Then I had written a class that worked something like this:
using UnityEngine;
using System.Collections;
public class MenuManager : MonoBehaviour
{
private int play = 0;
private int options = 1;
private int credits = 2;
private int scoring = 3;
private int quit = 4;
void OnEnable()
{
MenuArrowMovement.eMenuSelected += OnMenuSelected;
}
void OnDisable()
{
MenuArrowMovement.eMenuSelected -= OnMenuSelected;
}
void OnMenuSelected(int selection)
{
if(selection == play)
{
//PlayGame();
}
if(selection == options)
{
//ShowOptions();
}
if(selection == credits)
{
//ShowCredits();
}
if(selection == scoring)
{
//ShowScoring();
}
if(selection == quit)
{
//QuitGame();
}
}
}
And I thought to myself, this is probably where Enumeration comes in handy! And so I tried it:
private enum MenuOptions {Play, Options, Credits, Scoring, Quit};
if (selection == MenuOptions.Play)
//do stuff
That didn’t build, of course. Can’t compare int and enum.
But then what value does MenuOptions.Play contain, I thought it was supposed to be int by default?
Debug.Log(MenuOptions.Play);
Console writes Play
hm, ok. I expected it to be 0. I’m back to square one then.
What the heck do you use enums for if not for a situation like this, where you want to give numerical value to some variables, so that you can then have a more readable code? The only examples I see when searching the net are for week days, which are rather irrelevant to my needs.
So TL;DR Can I use enums in the example above to achieve my needs, and if so how?
And can someone provide an example where enums would be used in a script relating to a game, and not calendars.
Thanks for any advice!