- Home /
How do I use an enum value as a variable?
I'm trying to make a weapon cycle through two modes, and in order to do this I've created an enum for the two modes, and tried to equal an integer to the length of the enum. However, apparently I can't use the enum as a variable. Since I have only beginner knowledge of enums, can someone fill me in?
Code:
public enum PullModes {PullMode1, PullMode2};
public PullModes pullModes;
private int numberOfPullModes = System.Enum.GetValues(PullModes).Length;
using System;
enum Priority
{
Zero,
Low,
$$anonymous$$edium,
Important,
Critical
};
class Program
{
static void $$anonymous$$ain()
{
// New local variable of the Priority enum type.
Priority priority = Priority.Zero;
// Set priority to critical on $$anonymous$$onday.
if (DateTime.Today.DayOfWeek == DayOfWeek.$$anonymous$$onday)
{
priority = Priority.Critical;
}
// Write this if the priority is important.
if (IsImportant(priority))
{
Console.WriteLine("The problem is important.");
}
// See if Low priority is important.
priority = Priority.Low;
Console.WriteLine(IsImportant(priority));
// See if Important priority is.
priority = Priority.Important;
Console.WriteLine(IsImportant(priority));
}
static bool IsImportant(Priority priority)
{
// Switch on the Priority enum.
switch (priority)
{
case Priority.Low:
case Priority.$$anonymous$$edium:
case Priority.Zero:
default:
return false;
case Priority.Important:
case Priority.Critical:
return true;
}
}
}
Answer by Bunny83 · Jul 24, 2017 at 10:25 PM
When you declare an enum you actually create a new type. However every enum type has an underlying integral data type (sbyte, byte, short, ushort, int, uint, long, ulong). If no explicit type is specifed the default is "int". So any enum value can be casted into it's underlying type and back:
public enum MyEnum : int
{
A, B, C, D
}
// ...
MyEnum someEnumValue;
someEnumValue = MyEnum.B;
int val = (int)someEnumValue; // val == 1
someEnumValue = (MyEnum)2; // someEnumValue == "C"
Note that an enum value literally is the same as it's underlying value. You can even assign an integer value to an enum that doesn't have a corresponding "constant":
someEnumValue = (MyEnum)42; // no constant inside MyEnum for this value 42
A common way if you want a countable enum is to define a special "Count" or "Size" value as last element:
public enum Mode
{
M1, // == 0
M2, // == 1
M3, // == 2
M4, // == 3
Count // == 4
}
public Mode mode;
This way one can simply do:
mode++;
if (mode >= Mode.Count)
mode = Mode.M1;
This would increase the value by one each time and checks if we reached our additional "Count" value if so go back to the first. That way you have a cycle. M1 -> M2 .> M3 -> M4 -> M1 -> M2 -> ...
Note that this of course only works as long as your enum values are sequencial. Enum values can be specified however you like:
public MyEnum
{
A = 45,
B = 545,
D = 65536,
chicken = 1
}
Answer by tomandresen · Jul 24, 2017 at 09:09 PM
Am I correct in assuming you don't actually want to assign the length of the enum (which in the above example will always be 2)?
If you want to switch between the two modes, this is how to do it:
public void Start()
{
// Default to first pullmode
public PullModes pullmodes = Pullmodes.PullMode1;
}
public void Update()
{
SwapPullModes();
}
private void SwapPullModes ()
{
// Switch between the two pull modes
if (pullModes == PullModes.PullMode1)
{
pullModes = PullModes.PullMode2;
}
else
{
pullModes = PullModes.PullMode1;
{
}
This works fine. I do have one question, though: what would I do if I had more than 2 modes to the weapon?
Answer by c4ctus · Jul 24, 2017 at 10:02 PM
If you want to move the enum to the next value you can use this
public static class EnumExtensions
{
public static T Next<T>(this Enum e)
{
try
{
var enumInfo = Enum.GetValues(typeof(T));
var currentValue = e.ToString();
for (int a = 0; a < enumInfo.Length; a++)
{
if (((T)enumInfo.GetValue(a)).ToString() == currentValue.ToString())
{
if (a == enumInfo.Length - 1)
{
return (T)enumInfo.GetValue(0);
}
else
{
return (T)enumInfo.GetValue(a + 1);
}
}
}
}
catch (Exception ex)
{
Debug.Log("Error while read enum: " + ex.Message);
}
return default(T);
}
}
public class YourClass : MonoBehaviour
{
public enum PullModes { PullMode1, PullMode2, PullMode3 };
public PullModes pullModes;
public void Awake()
{
for (int i = 0; i < 10; i++)
{
pullModes = pullModes.Next<PullModes>();
Debug.Log(pullModes.ToString());
}
}
}
Your answer
Follow this Question
Related Questions
Use enum value as a index variable of a list 1 Answer
Save Integer Values 1 Answer
Divisible by 3 2 Answers