When do you use enum?

Hello developers! I am having a problem understanding when to use Enum and why you use it. Can you guys help me understand this?

if this is on the wrong category please tell me

4 Likes

Enum is for things that you can’t describe by strings (words), such as, Enum.LeftShift, Enum.UserInputType, etc… You mostly use them when you want to see what key the player is pressing, for example: if input.KeyCode == Enum.KeyCode.E then -- do stuff if they pressed the E key

12 Likes

You use enum to change properties and settings of some object it has a lot of classes

for more info go to:

https://developer.roblox.com/en-us/api-reference/enum

4 Likes

Enum is a datatype. Can think of it a bit like a bool (true/false). When a part of your code expects a true/false assignment, then anything else will cause problems. Enum works in a similar way, but you can have more than two options, and you can name the labels for the options whatever you want (if you are the one creating the enum). It’s basically a datatype that holds a list of valid options that are given descriptive labels.

2 Likes

For example:

Step.1

Step.2

:face_with_raised_eyebrow: What do you mean (true/false)??

Enum is kind of a list that holds properties.

This is what the website says:
An enumeration — sometimes shortened to “enum” — is a special data type that can take one of a set of values. For example, the Material enum represents the material type for a part.

In a script, enums are accessed through a global object called Enum . To get all of the EnumItem options available for an enum, call the GetEnumItems() method on an enum type:

5 Likes

What I meant there is that the built-in Boolean type has two valid options: true or false. Under the hood, a list of options like this is often tied to a sequence of numbers such as false = 0 and true = 1. With the Enum type, a programmer can create something similar to that true/false idea by creating their own list of labels that can be thought of as unique numbered indexes in a sequence.

Let’s see if an example makes my point more clear:

Let’s imagine that Lua didn’t have a true/false Boolean type (we couldn’t write things like local var = true). In that case we could create constants to fill the void:

local False = 0
local True = 1

local var = True -- can do this now

Rather than have a bunch of constants floating around like that, we could group them into a data structure and use them in a similar way:

BOOL = {False = 0, True = 1}

local isSmiling = BOOL.True
local isClapping = BOOL.True

if isSmiling and isClapping then
    print("Someone is happy.")
end

Enum is a datatype that can be thought of as expanding on that idea. It’s a way to show people working on some code that there are a discrete number of options (list of constants) that are valid in a given context.

enum = {Red = 1, Blue = 2, Green = 3, Orange = 4}
local colormap = {"255,0,0","0,0,255","0,255,0","255,170,0"}

print(colormap[enum.Orange])

For a Roblox example, we can look at Materials for a Part. There is a specific set of pre-defined labels (and associated number values) that are valid inputs when setting a Material. You can use the enum.label or often the material number that was defined by the programmer(s) directly, but you can’t just start inserting things into code that sets a Part’s material without breaking things. In some languages, using something other than a valid enum will generate an error before the code is run. In Lua, I think this is all mostly a convenience to show what the valid/expected options are for some property.

2 Likes

A nice and simple way to think about enums, is if the property you want to change in a script is a drop down type of property, such as material inside of a part, then use enum. I am not sure if that works for everything, but that is how I learned how to use enums effectively.

1 Like

Thats Table not Enum Dude :stuck_out_tongue: :stuck_out_tongue:

other people have different answers but the way I see it is just a replacement for strings when making some kind of “option” picker. and a good one, at that. instead of putting a string that says “up” “down” etc you can

  1. categorize it into an Enum called “Direction”
  2. get lint errors if you make a typo (at least for the built in ones, I think)
  3. I’ve also seen and use custom enums that attach properties to them (like Speed.High.Value = 0.1) or something like that. you have the bonus of substituting them with numbers too.

Enum is just an thing that holds values.

The reason why it was created was because the actual values are hard to remember so instead having it in one place and indexed is a lot easier and more convenient for us devs.

You don’t have to use enums.

Enum.DeviceType.Phone.Value == 3

If you were in a script and wanted to check for what device the user was using then using the enum is much more easier than remembering the actual value.

Basically enums are just for convenience.

2 Likes

:laughing: You are not wrong! Vanilla Lua doesn’t have an Enum type (pretty sure anyway), but enums can be implemented in Lua using Tables. That’s how you’d do it for your own code if you needed to make your own enums. The Enum datatype that Roblox has added may be implemented with Tables, but might also be passed through to the underlying C code (or whatever they’re using).

But yes, the examples I gave were Tables that illustrated the idea of an enum and a basic implementation if someone needed to create their own. The enum is a concept that’s roughly equivalent to a list of constants. The implementation is another matter.

The easy answer to the OP is to use Enums when and where Roblox expects to see them. Otherwise, think of them as lists of constants that can be implemented in your own code using Tables whenever you need to offer a discrete, finite list of possibilities for something (like for a class property).

3 Likes

Enum also short as Enumeration is used mostly for players information, for example, when used the function ‘Enum’, a list of items which are dedicated to that instance will show up, making it easier to control and manipulate.

1 Like