Using SWITCHES In Your Roblox Code? (MODULE REVIEW)

preview_player
Показать описание
In today's video, we take a first look at a module that creates switch statements for Luau and I give my thoughts on it.

🟣DISCORD SERVER🟣

💬SOCIALS💬

📅CURRENT UPLOAD SCHEDULE📅
Sunday, ~9:00-11:00 EST
Wednesday, ~9:00-11:00 EST

LINKS MENTIONED IN VIDEO:

WINDOWS POWERSHELL INSTALL COMMAND:

Thanks so much for watching! If you enjoyed, please consider subscribing and joining us in the Discord server, we'd love to have you!

――――――――――――――――――――――――――――――――

#roblox #robloxdev #lua #luau #scripting #robloxstudio #switches #switch #tutorial
Рекомендации по теме
Комментарии
Автор

This module feels redundant. Switch statements are meant to be cleaner if statements, and this just makes the code less readable. This is equivalent to adding a whole library to check if a number is even in Javascript

ana_s
Автор

In post of making this video I've spoken with the person who made the module again and they'll be adding the case statement fall-through functionality soon! Hope you all are having a great week.

summerequinox
Автор

A worse version of "If" statments

NewEdens
Автор

This module is actually very interesting, if statements usually look pretty ugly especially with a large number of statements

Master_A-A
Автор

i first learnt about switches when i first studied javascript

reminderIknows
Автор

this is my take on switch cases in luau, its not perfect but its as close as I could get it

local switch = function(value: any)
local returned = false

value = tostring(value)

return function(conditions: {[string]: ()->(any)})

for condition, callback in conditions do
if (condition == value) and not returned then
returned = callback()
end
end

if conditions.default and not returned then
conditions.default()
end

end
end

local MyValue = "hello"

switch(MyValue){
hello = function() -- both hello and whats up dont return and so default follows through
print("Hello !!!")
end,
whatsup = function()
print("Whats up")
end,

bye = function() -- this does return so default doesnt follow through
print("Goodbye :(")
return true
end,

["123"] = function() -- this is how it would work with numbers (the value passed is automatically converted to a string)
print("beep boop")
return true
end,

default = function() -- runs by default if no cases returned
print("How are you")
end,
}

jboythedev
Автор

i have a better solution for that
all u gotta do is just look for specific value in a table otherwise call default this aint that hard
```lua
local function switch(value, tbl)
local case = tbl[value] or tbl["default"];
if case then
case();
else
print("not able to cast the cases, consider using default case");
end
end

switch(123, {
[123] = function ()
print("Hello, World!");
end,
default = function ()
print("value not found");
end
});
```
to not make it redundant anymore
i dont even know why would anybody use switches in lua u can just use tables

BinaryMaestro
Автор

This isn't really a switch case
I feel like the closest thing you could get to a switch case cleanly would be using a table of functions with the key as a case. This is only really more efficient than many ifelse if you have more than ~10 or so functions

For a switch with no default

local cases = {
[1] = function()
print("hi")
end,
[2] = function()
print("hello")
end,
[3] = function(param)
print(param)
end,
-- more here, just an example
}

If you are sure the case will be in the list run this

local case = 2
cases[case]() -- prints "hello"

if you want to use a default, do this

case = 7
if cases[case] then
cases[case]()
else -- default is selected since case isn't in list
print("default")
end

This is nice because you can also pass any arguments to the functions within cases and even get a return from them as well. It also can really speed up your code if you have many many cases in the function table.

nicepotato
Автор

I learned about switches when i used java

ShadowClaw_Dev
Автор

This seems like it does the opposite of using an if statement It's meant to be more readable, but it just makes the script it more cluttered. Not to mention that you would need to explain to people you're working with, why you're not using if statements or what each switch case is doing etc.

Levnt
Автор

Somehow made switch case OOP... these lua devs man

buysnoah
Автор

Hello, New upload? Dont mind if i do!

Mono_DevRBX
Автор

You're a serial killer for using vs code for roblox

DynoDeso
Автор

Very bad implementation ngl, if you want a switch in lua just make a table of functions and index it like this: the cases should be constant and global, which makes it have a time complexity of O(1) as they're only loaded once. if you want to switch on a string i suggest you hash it, this way there wont be any collisions with the default case
cases = {
[1] = function(FallThrough)
print("Case 1");
end,
[2] = function(FallThrough)
print("Case 2");
FallThrough(3);
end,
[3] = function(FallThrough)
print("Case 3");
end,
default = function(FallThrough)
print("Default");
FallThrough(4);
end,
[4] = function(FallThrough)
print("Case 4");
end,
}

function doTableSwitch(value, cases)
local case = cases[value];

local function FallThrough(case)
cases[case](FallThrough);
end

return case and case(FallThrough) or cases.default(FallThrough);
end

doTableSwitch(5, cases);

alminecrafteano