Advertisement
LDDestroier

Switch case in Lua

Nov 27th, 2020
1,436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.57 KB | None | 0 0
  1. -- switch case in Lua
  2.  
  3. switch = function(check)
  4.     return function(cases)
  5.         if type(cases[check]) == "function" then
  6.             return cases[check]()
  7.         elseif type(cases["default"] == "function") then
  8.             return cases["default"]()
  9.         end
  10.     end
  11. end
  12.  
  13. -- example
  14.  
  15. favoriteFood = "pilaf"
  16.  
  17. switch(favoriteFood) {
  18.     pilaf = function()
  19.         print("That's some good rice")
  20.     end,
  21.     beef = function()
  22.         print("Best when cooked")
  23.     end,
  24.     tetris = function()
  25.         print("That's not a food")
  26.     end,
  27.     default = function()
  28.         print("I don't get it")
  29.     end
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement