C's switch case functions in lua.
- For normal Lua, just download the "SwitchCase.lua" into your workspace.
- For Luau, create a ModuleScript and copy the code into it
There are two ways to import the functions. I don't know why I did this. I was bored.
#1(Needs getfenv()):
require("SwitchCase").init()#2:
local SwitchCase = require("SwitchCase")
local switch, case, default = SwitchCase.switch, SwitchCase.case, SwitchCase.defaultThe first way might not work in some ways because I was just testing something and it happened to work. If you use #1, it is suggested that you write this to silence any errors:
switch, case, default = nil, nil, nil -- **NOT LOCAL**Here is an example of using this:
(Normal Lua):
switch, case, default = nil, nil, nil
require("SwitchCase").init()
local value = 10
switch (value) {
case (1) {
function()
print("The value is 1")
end
},
case (10) {
function(val) -- first argument is the value
print("The value is "..val)
end
},
default {
function(val)
print("Unknown Value("..val..")")
end
}
}(Luau):
switch, case, default = nil, nil, nil
require(game.ReplicatedStorage.SwitchCase).init()
local value = 10
switch (value) {
case (1) {
function()
print("The value is 1")
end
},
case (10) {
function(val)
print("The value is "..val)
end
},
default {
function(val)
print("Unknown Value("..val..")")
end
}
}This is a very simple module that was coded in like 30 minutes so there might be some bugs.