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
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
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
You use enum to change properties and settings of some object it has a lot of classes
for more info go to:
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.
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:
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.
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.
Thats Table not Enum Dude
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
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.
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).
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.