[:>·] LuaEnum
LuaEnum.rbxm (contains PackageLink) (2.9 KB) | Marketplace
LuaEnum is a module which you can use to easily create Enums like ones Roblox have in the Enum
global.
I made this module after seeing alot of recreations of Roblox’s enums which aren’t fully typed/require tedious typing to fully type, whilst with LuaEnum you only need to create 1 definition for everything!
Documentation
Theres only 2 functions in the LuaEnum module, one for creating singular LuaEnums and one for creating multiple at once (for the convenience of not having to type the names of every LuaEnum twice)
-
LuaEnum.new<EnumMembers>(name: string, fn: (createItem: callback) -> T): EnumMembers & LuaEnum
This is the function used to create individual LuaEnums, maybe if you only need 1 LuaEnum and not multiple.You pass the name you want your LuaEnum to be, and a function which should return your list of LuaEnumItems, like so:
ThecreateItem
function accepts a value for your LuaEnumItem (must be unique and a number or else the function will error) and an optionalprop
, which is going to be the value for theProperty
field, and returns a LuaEnumItem.
You can use theProperty
field to store values for the LuaEnumItem you may use later, like maybe a function which returns something, a number which describes a time interval, or a description.
Once that’s done, the function will return a LuaEnum which will have the LuaEnumItems andProperty
fields filled out.
-
LuaEnum.buildEnums(fn: callback)
This function works very similarly to LuaEnum.new(), but you can create multiple LuaEnums with it and it’ll return a table containing all of them. The main benefit of this function is that you don’t need to pass the LuaEnum’s name to thecreateEnum
function, it’ll be inferred by its key.
To use this function, you need to pass a function which accepts acreateEnum
function, and returns a dictionary containing all your LuaEnums, like so:
You can use thecreateEnum
function passed into your callback to create individual LuaEnums (without passing the name), and from there it’s the same steps as LuaEnum.new().
When it’s all done, your LuaEnums will be fully typed and usable in runtime!
Example
This is an example of creating a list of enums using LuaEnum.buildEnums()
Enum.luau
:
Script.luau
(requires Enum.luau
):
Full autocomplete:
Happy coding!