Search Unity

Is there a way to use numbers for enums?

Discussion in 'Scripting' started by Marscaleb, Dec 19, 2014.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    Okay, the point of using an enum is to have understandable designations for these variables, instead of using numbers.

    But I want to make a debug script that would systematically test each iteration of an enum I have created. This would require me to create a large switch event with a case for every possible type within that enum. It would be a lot easier if I could supply a number and have it convert that into the enum, and thus test out each iteration with a simple loop.
     
    KC_GAMING_STUDIO likes this.
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. enum Stuff {Alpha, Bravo, Charlie}
    2.  
    3. void Start () {
    4.     foreach (var stuff in System.Enum.GetValues (typeof(Stuff))) {
    5.         Debug.Log (stuff);
    6.     }
    7. }
    --Eric
     
    BenZed likes this.
  3. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Eric's answer is correct. The reason this works as that internally, enums are stored as integers, anyway.

    This code:

    Code (CSharp):
    1. enum Stuff {
    2.      Alpha,
    3.      Bravo,
    4.      Charlie,
    5. }
    is syntactically equivalent to

    Code (CSharp):
    1. enum Stuff {
    2.      Alpha = 0,
    3.      Bravo = 1,
    4.      Charlie = 2,
    5. }
    enums are a very handy data structure, and important to learn more about:
    http://msdn.microsoft.com/en-us/library/sbbt4032.aspx
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Well, technically, the reason it works is because it's iterating over a collection. It doesn't matter if they're numbers or not, although basically they are, of course. Also it doesn't matter if the enums are numbered consecutively. You can iterate over the enum as a string array with GetNames:

    Code (csharp):
    1. enum Stuff {Alpha = 5, Bravo = 8, Charlie = 12}
    2.  
    3. void Start () {
    4.     foreach (var stuff in System.Enum.GetNames (typeof(Stuff))) {
    5.         Debug.Log (stuff);
    6.     }
    7.     foreach (var stuff in System.Enum.GetValues (typeof(Stuff))) {
    8.         Debug.Log ((int)stuff);
    9.     }
    10. }
    --Eric
     
    BenZed likes this.
  5. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    I know that enums are numbers, but that still doesn't mean I can pass an integer to a function that asks for an enum.
    What I didn't know about was the option to return those names with System.Enum.GetNames as demonstrated above.
    Thanks for the solution!
     
  6. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    And, for future reference, your example would work with a cast!
    Assuming Eric's above definition for stuff:

    Code (CSharp):
    1. //In the case of a function that takes an Enum
    2. void SomeMethod(Stuff stuff){
    3.      //whatever
    4. };
    5.  
    6. SomeMethod((Stuff)5); //would send Stuff.Alpha to the function