You can do this as a simple if/else block, like so:
Code: Select all
local a = 3
if a == 1 then --case 1
print("a = 1")
elseif a == 2 then --case 2
print("a = 2")
elseif a == 3 then --case 3
print("a = 3")
else --default
print("No other option fits")
end
BUT you can also make it a function, to make things simpler for you (if you're too used to Java or similar):
Code: Select all
local a = 3
switch(a,
{1, print, {"a = 1"}},
{2, print, {"a = 2"}},
{3, print, {"a = 3"}},
{print, {"No other option fits"}})
function switch(...)
local args = {...}
local a = args[1] --Your actual value
for i = 2, #args-1 do --for every "case"
local v = args[i]
if a == v[1] then
v[2](unpack(v[3]))
return
end
end
args[#args][2](args[#args][3]) --"default" thing
end
Didn't work TOO much on this, but should work just fine. Here's how it works:
switch(number,
{case 1, function, arguments (in a table)},
{case 2, function, arguments (in a table)},
{case 3, function, arguments (in a table)},
{function, arguments (in a table)} --default
)
This can be done in many ways (without tables, automatically detecting no arguments or single arguments, etc.), but I just wanted to give a fast example. I'm pretty sure a more experienced coder can do something better. I hope it works for you