I have few enums in my game, and I know that I can give them integer values:
enum DamageType{
physical = 0,
magic = 1,
fire = 2,
…
}
and I know I can use these integer values by using (int)DamageType.physical (by putting an (int) before the enum). However, how can I do it the other way around - I get the enum’s value by using an int?
And can I get the amount of members the enum contains with a function? (something like getting an array’s length)?
Or perhaps can I loop trough each member (by using for or while)?
You simply cast the integer value to your enum:
DamageType dt = (DamageType)yourIntValue;
To get the length of an enum you use System.Enum like so:
int enumLength = System.Enum.GetValues( typeof( DamageType )).Length;
1 Like