Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESX.Switch function #1595

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ name: Bug report
about: Create a report to help us improve
title: "[Bug] - esx_script - Issue"
labels: bug
assignees:
- TheFantomas
- Gellipapa
assignees: Arctos2win, Kenshiin13

---

Expand Down
4 changes: 1 addition & 3 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ name: Feature Request
about: Help us improve esx with your ideas
title: "[Feature Request] - esx_script - Add better configuration"
labels: enhancement
assignees:
- TheFantomas
- Gellipapa
assignees: Arctos2win, Kenshiin13

---

Expand Down
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
Loading