EnumList Module - Create custom Enums for easier development

Hello all,

I have developed a simple module that allows you to easily create your own Enums and use them in your development. You can download the file below and view a short documentation.

Feel free to reply with your feedback, I am a 14-year-old developer and this is one of my first resources I have made for the community. I am always looking to improve.

FILE

EnumList.rbxm (4.7 KB)

DOCUMENTATION

Module

The first step to using any module, of course, is to require it. Can’t really access it otherwise.

local EnumList = require(game.ReplicatedStorage.Packages.EnumList);

From here, you are given a few simple functions to start creating:

Module methods

EnumList.new

EnumList.new(name: string, items: { string }): EnumList

This function creates a new EnumList object.
The name parameter is the name of your EnumList- for example, Enum.NormalId’s name is “NormalId”.
The items parameter is an array of strings that will become your EnumListItems.


EnumList:GetEnumList

EnumList:GetEnumList(name: string): EnumList

Fetches a previously created EnumList that matches the specified name.


EnumList:GetEnumLists

Syntax:

EnumList:GetEnumLists(): { [string]: EnumList }

Returns a dictionary of every single created EnumList where each key is the name of an EnumList, and each value is the corresponding EnumList.


To view more info about each class, open the next category below.

Classes

EnumList

{
  "ItemOne": EnumListItem,
  "ItemTwo": EnumListItem,
  "ItemThree": EnumListItem ...
}

An EnumList object contains a list of EnumListItems.

Properties

None

Methods

EnumList:GetName

EnumList:GetName(): string

Returns the name of the EnumList.


EnumList:GetEnumItems

EnumList:GetEnumItems(): { EnumListItem }

Returns an array containing every one of the EnumListItems inside the EnumList.


EnumList:GetEnumNames

EnumList:GetEnumNames(): { string }

Returns an array containing every one of the EnumListItems’ names inside the EnumList.


EnumList:FromName

EnumList:FromName(name: string): EnumListItem

Looks for an EnumListItem that has name as it’s name and returns it.


EnumList:FromValue

EnumList:FromValue(value: number): EnumListItem

Looks for an EnumListItem whose numeric value matches value.


EnumList:Delete

EnumList:Delete(muteWarnings: boolean): TrashedEnumList

Deletes all traces of the EnumList, and returns a copy of it with no methods. (only EnumListItems)


Extra Cases

EnumList(_: string?)

@return - if (_ == nil) then EnumList:GetEnumItems() else EnumList[_]

tostring(EnumList)

@return - "EnumList.{ListName}"

EnumListItem

{
   "Name": string,
   "Value": number,
   "EnumType": string,
   "Identifier": string
}

An EnumListItem object is a child object to an EnumList.

Properties

EnumListItem.Name

A string containing the EnumListItem’s name.


EnumListItem.Value

A numeric value to make a EnumListItem unique from others in an EnumList.


EnumListItem.EnumType

A string defining the EnumListItem’s parent EnumList.


EnumListItem.Identifier

A string defining the path of the EnumListItem. Formatted as EnumList.{ListName}.{ItemName}({ItemValue}).


Methods

EnumListItem:GetEnumList

EnumList:GetEnumList(): EnumList   

Returns the EnumListItem’s parent EnumList.


EnumListItem:IsA

EnumList:IsA(_: string): boolean

Returns true if the EnumListItem belongs to an EnumList with the name _.


Extra Cases

EnumListItem()

@return - EnumListItem.Value

tostring(EnumListItem)

@return - "EnumList.{ListName}.{ItemName}"

EnumListItem == Value

@return - EnumListItem.Identifier == Value.Identifier
Examples

Creating a new list

local EnumList = Packages.import("EnumList");
local CarBrand = EnumList.new("CarBrand", {
   "Volvo",
   "Volkswagen",
   "BMW",
   "Audi",
   "Ford",
});

Retrieving a list from a different script

local CarBrand = EnumList:GetEnumList("CarBrand");  

Item comparison

local CarBrand = EnumList:GetEnumList("CarBrand");  
      
local playerA_car = CarBrand.Volvo;
BindableEvent.Event:Connect(function(playerB_car): ()
   print(playerA_car == playerB_car)
end);
BindableEvent:Fire(CarBrand.BMW); --> false
BindableEvent:Fire(CarBrand.Ford); --> false
BindableEvent:Fire(CarBrand.Volvo); --> true
4 Likes