Skip to content

Commit

Permalink
Add ESX.Switch function
Browse files Browse the repository at this point in the history
  • Loading branch information
Zykem committed Jan 14, 2025
1 parent 11d3c72 commit 3589ed5
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions [core]/es_extended/shared/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,31 @@ function ESX.Await(conditionFunc, errorMessage, timeoutMs)

return false
end

---@param switchValue any The value that matches the case to execute.
---@param caseTable table A table mapping values to function references (cases).
---@param defaultFunction? function|nil Optional. default function to execute if no case matches.
---@param ... any Optional. Additional parameters to pass to the matched function (or default function)
---@return any: Result of the executed function (case or default).
function ESX.Switch(switchValue, caseTable, defaultFunction, ...)
assert(switchValue ~= nil, "'switchValue' parameter cannot be nil!")
ESX.AssertType(caseTable, "table")

if defaultFunction then
assert(ESX.IsFunctionReference(defaultFunction), "'defaultFunction' must be a function reference!")
end

local caseFunction = caseTable[switchValue]

if caseFunction and ESX.IsFunctionReference(caseFunction) then
local success, result = pcall(caseFunction, ...)
assert(success, ("Error executing case for value '%s': %s"):format(tostring(switchValue), result))
return result
elseif defaultFunction then
local success, result = pcall(defaultFunction, ...)
assert(success, ("Error executing default function: %s"):format(result))
return result
else
return false
end
end

0 comments on commit 3589ed5

Please sign in to comment.