Questions about enumerations

According to the roblox wiki on enums:

An enumeration — sometimes shortened to “enum” — is a special data type that can take one of a set of values.

Each one of the Enums Indexes documentation looks like this (this is for Enum.HumanoidRigType):

Name Value Description
R6 0 Indicates that a character is using the legacy R6 rig.
R15 1 Indicates that a character is using the new R15 rig.

This led me to wonder, if an Enum is just a set of values, how would the values work? I usually call the Enums like this

if Textbox.Font = Enum.Font.SourceSansBold then
    --more code goes here
end

I never really get to use the “value” part of the enum. For example, using a simple assertion test we get:

assert(Enum.HumanoidRigType.R15 == 1) --false

So obviously, its not the actual number. But is the enum a dictionary of some sorts?

Enum.HumanoidRigType = {
    ["R6"] = 0,
    ["R15"] = 1
}

This does not seem right though. You can tell I am confused with the definition of the enumeration because I used them for years without knowing what they fundementally are.

An enumeration is a complete, ordered listing of all the items in a collection. … The term is commonly used in mathematics and computer science to refer to a listing of all of the elements of a set.

I found this when looking into the definition behind the word and I think it makes sense. Most Enums are used to define pre-made constants. But I don’t really know what they truley are, this is just a guess.

That allows you to do Enum.HumanoidRigType.R15.Value and get 1 or Enum.HumanoidRigType.R15.Name and get “R15”. Enums are Enums, not tables or anything else. It’s like an Instance; it’s just a Roblox thing.

5 Likes

So that makes Enums a IntValue? Because when I do IntValue.Value I usually get a number.

It is userdata read here

1 Like

From what I understand, Enumerations are a form of a dictionary in a sense.

When calling something like Enum.HumanoidRigType.R15, it doesn’t make sense to store to a variable “R15” or “RigTypeR15”, so the variable stored is an integer to separate out the indexes (R15 and R6).

When coding in other languages, such as C++, I typically specify Enumerations quite often for different states. Normally do so similarly to this:

local EnumObject = {}
EnumObject.R15 = 1
EnumObject.R6 = 2

return EnumObject

This example would be used in a module script, but can be modified to your use case.

The differentiation occurs with the use of the index, but any complicated math applied will use the integer specified, because it just doesn’t make sense to use the alternative.

This also explains why you can determine priority levels with the AnimationPriority Enum.

Hope this helps your understanding.

No, they’re Enums. Not Instances, not tables, just Enums.

3 Likes

I assume they are related to C++ enums? So Enum.HumanoidRigType is equivalent to:

#include <iostream>

enum HumanoidRigType {R6 = 0, R15 = 1};
HumanoidRigType rig = R15;
int otherRig = R6;

std::cout << otherRig -- prints 0