A chainable switch for FiveM Lua.
Copy switch.lua into your resource and load it in your fxmanifest.lua:
lua54 'yes'
shared_script 'switch.lua'lua54 'yes' is required.
switch(value) starts a chain. :case(...) takes one or more values followed by a handler, and the first matching case wins. :default(fn) runs if nothing matched. :get() returns whatever the handler returned.
local rate = switch(GetVehicleClass(veh))
:case(8, function() return 0.6 end)
:default(function() return 1.0 end)
:get()Cases are checked top to bottom and the first match wins.
local label = switch(GetVehicleClass(veh))
:case(8, function() return 'motorcycle' end)
:case(13, function() return 'bicycle' end)
:case(14, function() return 'boat' end)
:case(15, function() return 'helicopter' end)
:case(16, function() return 'plane' end)
:default(function() return 'car' end)
:get()local ok, reason = switch(state)
:case('locked', 'broken', function() return false, 'unavailable' end)
:default(function() return true end)
:get():get() returns every value the handler returned.
local function getWheels(class)
local _, _, wheels <const> = switch(class)
:case(8, function() return 'bike', 0.6, 2 end)
:default(function() return 'car', 1.0, 4 end)
:get()
return wheels
endWrap the call in parentheses to keep only the first value:
local function getLabel(class)
return (switch(class)
:case(8, function() return 'bike', 0.6, 2 end)
:default(function() return 'car', 1.0, 4 end)
:get())
endInline handlers can use surrounding locals directly. Any extra arguments given to switch are passed to the handler, which is useful when the handlers are named functions defined elsewhere:
local function policeBuy(src, item, amount, price) ... end
local function deny(src) ... end
switch(job, source, item, amount, price)
:case('police', policeBuy)
:default(deny)