Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Why would you use an enum?

Discussion in 'Scripting' started by Caelorum, Mar 17, 2015.

  1. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    This is just a question out of curiosity. I'd say i'm somewhat intermediate with coding, and i'm trying to get some solid coding practices down. But there are a few things about coding i don't fully understand the "Why" behind.

    For instance, why would you use an enum, with 4 different variables inside it, instead of just declaring 4 different public variables? Isn't it technically the same thing? For what reason in a game would I need to do that?

    I understand it could just be for space i guess, the same reason you would make functions that can repeat. But an enum doesn't really seem to save any space. You are still re declaring the enum into a variable. Wouldn't it just save time to just use a regular variable?

    If this seems ignorant to some, it's because it is. I don't really understand why certain things are used in code, and i'm curious about their "Why" applications.
     
  2. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    You can create a class of the Enum type and use it everywhere. It doesn't need to bee declared in a variable inside a script.
     
    borstadam likes this.
  3. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    According to Unity Learn, you have to redeclare it to use it.

    E.I. One = Enum.number;

    Then you can use One, to describe that value of number inside the enum.

    When you could just make int One = 1;
    Or just make the class a singleton and use the class and it's variables whenever or wherever you want.
     
  4. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    ^^Yup. And also, suppose you want to write a weapon class. The weapon needs a weapon type variable, like melee, ranged, laser or whatever. You could say that weapon type is an int, with 0 being melee and so on. That would mean, however, that you would always have to remember which number is which type when comparing or setting it. It would be much nicer if you made the weapon type an enum which acts like an int as well, but you can give actual descriptions/names to each number, so when you want to compare or set it, you can write the right type or even use the auto complete function of MonoDevelop to give you a list of possible enum element names.
     
    RoiIam likes this.
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824

    You could use constants as well (e.g. ints and compare your values with those) instead of enums.
    BUT!!!! One thing is that you definitely know which values are available (good for representation of states). With constants of another type it would be possible to have 'invalid' values.
    Also, specificly in Unity you'll get a dropdown menu when they're shown in inspector.

    Works with constants too :p
     
  6. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    As an example, one area I use Enums in for my game is to represent block faces. It's a voxel game, and I have to calculate a texture for each block face and determine if the given face is solid or not.

    I make an enumeration:

    Code (CSharp):
    1. public enum Face { Left, Right, Front, Back, Top, Bottom }
    And then I can have my methods that calculate the textures take in a Face.

    Of course, I could use constants such that FaceLeft = 0, FaceRight = 1, etc. But this way I don't have to do any checks that say something such as: if face > 0 && face <= 6... or if face < 0 || face > 6, return some error (or clamp it).

    Technically, I could just tell myself I'm sure I won't mess that up since I'll always use the constants when I call my function. I was always confused about these kinds of things as well when I was starting out. But I have learned that it is not good practice to think in that way. You get things like: what if other people help develop later? What if you make a typo and put the wrong thing in (because you didn't use the constants for whatever reason), etc.

    I just use the enumeration. It's not that much more work to use it as opposed to not using it, so why not?
     
  7. AndyLL

    AndyLL

    Joined:
    Aug 25, 2013
    Posts:
    75
    1) Strongly typed. - if you have a parameter that takes an enum instead of an int then you are forced to only pass in one of the enum values

    2) IEnumerable - you can loop through the valid enums

    3) enum.ToString() - you can get the text representation of the enum
     
  8. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    I'd like to add on that there's another reason to use them as well - bit flags. I don't have the time to explain that at the moment, though. So if someone else doesn't explain it, a simple Google search should tell you how that works.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    enums are very useful for handling discrete data that doesn't easily fall into one of the existing data types. For example damage type might be fire, earth, air or water. An enum lets you hand these around with DamageType.water, instead of doing string comparisons, ints or the like.

    String comparisons are bad because they are slow and prone to typos. ints are bad because they are prone to typos, its also difficult to remember what each int stands for.

    enums have the best of both worlds, they are as fast as ints, but are human readable like strings.
     
    RoiIam and AndyLL like this.
  10. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
    Also, Intellisense. Type a dot after the type, get a list in the editor. And you get a drop-down list of items in the Unity inspector.
     
    knr_, AndyLL and Kiwasi like this.
  11. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    IMO the best way to see why Enums are awesome is to identify them in libraries you use. A great example is tweening libraries like DOTween. When you create a tween one of the parameters is EaseType (basically the equation that determines the movement). There are about 10 or so options. These could be represented just with an integer value but then I would have to check the documentation to make sure I knew what number it was. With enums it's much easier because I just type EaseType.InOutQuad or something.

    Like most things in coding, Enums are just one way to solve certain problems and you could get by without ever using them but they're a nice and clean way to represent certain kinds of data.
     
  12. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I use enums relentlessly for state machines and ai behaviour, for example:

    public enum State{Idle, Aggro, Combat, FindHealth, Flee}
    public State state;

    Then in code..

    switch (state)
    {
    cases...
    }

    The idea is it's very helpful to you as a human. You can cast to and from enum to (int) without any issues as well.
     
    TheValar likes this.
  13. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    That's probably the most legitimate reason I end up using enums. Really, the point of them is to turn abstracted integer values into something more human readable. Instead of saying a tile type is a value of 2, it's value is grass, even though it's value will read as an int of 2.

    Generally speaking, using enums as bit flags for a bunch of boolean properties is one of their best uses. At the very least, it's one of the best use cases that you aren't likely to hate yourself later for having used. It's easy to use enums for prototyping and later find out that they don't scale well.
     
  14. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It's a good point about bit flags. I used enums to tag different tiles for an auto tile map, and it was trivial to sort into results.