[1.0.0] CustomEnums - Create your custom enum

This is a module I’ve made that allows custom enums.
I call it custom enum.This is similar and based on C#'s enum.

What does this do?

It’s just a custom enum value, with features like comparing enums. This is more powerful than a table.

How do I create one?

Simple, just do something like this

local seasons = customenum { "Spring", "Summer", "Autumn", "Winter" };
print(seasons.Spring);
print(seasons.Spring.ToInt32());

Spring
0

note that it’s zero based index instead of one based index

Alternatively you can do somehting like this

local options = customenum
{
	option1 = 0b0001,
	option2 = 0b0010,
	option3 = 0b0100,
	option4 = 0b1000,
};

Similar to C#

public enum options
{
	option1 = 0b0001,
	option2 = 0b0010,
	option3 = 0b0100,
	option4 = 0b1000,
}

You can set it to any integer from -2 147 483 648 to 2 147 483 647
Please note it’ll throw an error if the character has any other characters other than letters, numbers and underscores (_) or if it begins with a number or it’s a reserved keyword.

local error = customenum { '1this', 'will!', 'throw an', 'error!', 'if' }

will throw an error

More parts will be documented later.

Where do I get this?

You can download this via GitHub or you can get it here:
1.0.0:

  • File (5·74 KiB)
  • Released: 14 May 2020, 01.05.05 (CET)
  • BSD 2-clause licence
48 Likes

Is there a case where you can think of where it would be useful to use? I don’t find a use to it but it’s great!

1 Like

Think enums as radio buttons, you can ensure it’s in the value unlike integers and strings.
This module is inteded for C# or Java users who uses the enum keyword a lot like me. This is just a habit, hung over when I program in C#.

These should help:

2 Likes

An enum (short for “enumeration”) is a set of constant values. For instance, the Material property of a part is an enumeration. With the material it can only be one of the many Roblox provides us. Can’t be anything else. Strings and numbers are too arbitrary, so that is where enums come to the rescue.

6 Likes

Thanks for this :grin: I was wanting custom enums in roblox for a long time. One question, can you return enums from a function like so?

function ReturnEnum()
   return Enums.State1
end

print(ReturnEnums)
1 Like

Yes, that is possible, although your code would obviously need

local ReturnEnums = ReturnEnum()

to work :slight_smile:

3 Likes

thanks for the heads up , I hate using strings instead of enums :+1:

1 Like

Would be super good if you could expand the enum like:

local customenums = require(script.Parent.CustomEnum)
local Type =  customenums {"Weapons"}
local WeaponType = Type {"Long Sword", "Bow"}
1 Like