Using SWITCHES In Your Roblox Code!

Поделиться
HTML-код
  • Опубликовано: 28 окт 2024

Комментарии • 45

  • @ana_s
    @ana_s 2 месяца назад +27

    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

    • @summerequinox
      @summerequinox  2 месяца назад +7

      Totally valid take. For me, personally, I think there could be some nicer uses for it once the case fall throughs get added (really only time I use switches is when I’m seeking this functionality). Thanks for your comment ❤️

    • @whupass
      @whupass 2 месяца назад +5

      Honestly such a useless implementation

    • @itso6778
      @itso6778 2 месяца назад

      when switch cases are compiled in other languages it has a chance of being optimized in a way that is faster than the traditional if statement check depending on whats under the cases. Like equinox said it would've been better to add a break statement, would make it more useful

  • @alminecrafteano
    @alminecrafteano 2 месяца назад +13

    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");
    return FallThrough(3);
    end,
    [3] = function(FallThrough)
    print("Case 3");
    end,
    default = function(FallThrough)
    print("Default");
    return FallThrough(4);
    end,
    [4] = function(FallThrough)
    print("Case 4");
    return 69;
    end,
    [8] = function(FallThrough)
    print("Case 8");
    end,
    }
    function doTableSwitch(value,cases)
    local case = cases[value];
    local function FallThrough(case)
    return cases[case](FallThrough);
    end
    return (case or cases.default)(FallThrough);
    end
    print(doTableSwitch(2,cases));

  • @NewEdens
    @NewEdens 2 месяца назад +16

    A worse version of "If" statments

    • @alminecrafteano
      @alminecrafteano 2 месяца назад

      Switch cases are actually faster than else ifs if implemented correctly, this is just retarded ngl

  • @C3MN12
    @C3MN12 2 месяца назад +1

    This dude is underrated

  • @_barskii
    @_barskii 2 месяца назад +1

    might start using switches, because they seem very useful. I just need to look into them more so I have the full understanding of what they do. I think switches could improve my code a lot.

  • @reminderIknows
    @reminderIknows 2 месяца назад +2

    i first learnt about switches when i first studied javascript

  • @nicepotato5755
    @nicepotato5755 2 месяца назад +1

    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.

  • @Master_A-A
    @Master_A-A 2 месяца назад +2

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

    • @summerequinox
      @summerequinox  2 месяца назад +1

      I think it’s interesting as well!

  • @lukeewhy
    @lukeewhy 2 месяца назад +1

    Interesting

  • @baubobbi371
    @baubobbi371 2 месяца назад +1

    Oh man... Dude there is no shot that I wanted to make the exact same module 2 weeks ago!...

  • @blendedphoenix
    @blendedphoenix Месяц назад

    What i dont like about this implementation is constructing the case at run time or within the code.
    An easier format:
    Local caseObj = CaseFactory:new(
    function(value) if value ~= 5 then return value end
    -- if you return a value it flows to the next case in the order.
    -- if you return a value and false then it will return a final value back at casecall and not go any farther in case values
    -if you return nothing it will stop there and not flow down.
    end,
    function(value) if value ~= 7 then return value end
    end
    )
    Feels like the implemebtation is icer structured and since its done at run time vs pre defined and used i feel like that is problematic.
    Doing it this way makes it more readable, flexble and accounts for all the key controls you want. As well as allow q case to perform a transform and pass it through cases if you wanted as a result of.
    And ifnit was me i may include some quick preconstrucyed functions i vould pass in and fill out that would do the work for me.

  • @Levnt
    @Levnt 2 месяца назад

    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.

  • @summerequinox
    @summerequinox  2 месяца назад

    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.

  • @exolyne.
    @exolyne. 2 месяца назад +1

    I know a trick that allows for cool syntax:
    local function switch(case)
    return function(cases)
    (cases[case] or cases.default)()
    end
    end
    switch (1) {
    [1] = function()
    print("One!")
    end,
    [2] = function()
    print("Two!")
    end,
    default = function()
    print("Something else!")
    end,
    }

  • @1BP6
    @1BP6 2 месяца назад

    I can see the use of this for long processes, this would be cleaner than a bunch of if statements, by just using tables u can have a more cleaner and smaller (maybe less readable) script.
    Simple Switch: (If continue is set to 0, the while loop will stop, otherwise make sure its 1)
    type SwitchValue = {Value: any, Function: callable, Continue: number}
    return function(Value: any, conditions: {[SwitchValue]: () -> (any)})
    local x=#conditions-1;
    while x>-1 do
    if conditions[#conditions-x][1]>=Value then
    conditions[#conditions-x][2]()
    x *= conditions[#conditions-x][3]
    end
    x-=1
    end
    end

    • @1BP6
      @1BP6 2 месяца назад

      Simple Usage:
      local switch = require(script.switch)
      local MyInteger = 5
      switch(
      MyInteger,
      {
      {1,function() print(1) end,1};
      {2,function() print(2) end,1};
      {3,function() print(3) end,1};
      {4,function() print(4) end,0};

      {5,function() print(5) end,1};
      {6,function() print(6) end,0};

      {7,function() print(7) end,1};
      {8,function() print(8) end,1};
      {9,function() print(9) end,0};
      }
      )

  • @jboythedev
    @jboythedev 2 месяца назад +2

    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,
    }

    • @alminecrafteano
      @alminecrafteano 2 месяца назад

      slow, don't do a for loop

    • @jboythedev
      @jboythedev 2 месяца назад

      @@alminecrafteano right idk how i didn't notice that

    • @thessuperrooster5006
      @thessuperrooster5006 2 месяца назад

      just use **if** this is overcomplicated

  • @Rusterdev5
    @Rusterdev5 2 месяца назад

    what's your vscode theme?

  • @BinaryMaestro1
    @BinaryMaestro1 2 месяца назад +1

    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

  • @ShadowClaw_Dev
    @ShadowClaw_Dev 2 месяца назад +2

    I learned about switches when i used java

  • @Mono_DevRBX
    @Mono_DevRBX 2 месяца назад +1

    Hello, New upload? Dont mind if i do!

    • @summerequinox
      @summerequinox  2 месяца назад

      Hey Mono!

    • @Mono_DevRBX
      @Mono_DevRBX 2 месяца назад

      @@summerequinox Hello! I'm working a lot on that module!

  • @PrismFave
    @PrismFave 2 месяца назад

    using if else for substituting luau not having switch makes me feel like Yandere Dev :(

  • @exotic42069
    @exotic42069 2 месяца назад

    using switches in lua is just stupid because there arent switches in lua normally so its alot worse + its alot slower

  • @DynoDeso
    @DynoDeso 2 месяца назад +1

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

  • @buysnoah
    @buysnoah 2 месяца назад

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

    • @OcnarfPro
      @OcnarfPro 2 месяца назад

      For real

    • @buysnoah
      @buysnoah 2 месяца назад

      @@OcnarfPro shut up ocnarf

  • @real_quandie
    @real_quandie 2 месяца назад

    it seems pointless