Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR adds a new ESX.Switch function. It enhances the existing logic by enabling flexible argument passing to case functions and improving error handling. The function is similar to the
switch
statement found in other programming languages like JavaScript, offering an elegant way to handle multiple conditions. It allows handling various cases based on a given value.Motivation
Motivation of this PR is to simplify and clean up decision-making logic. Instead of constnatly using if-elseif blocks, it offers a more readable and maintainable way of handling cases.
Implementation Details
It was implemented to receive a value, a table of possible cases, an optional default function and extra arguments that are passed to the default function (if exists).
Each case is a key-value pair in caseTable, where the key corresponds to a value and the value is a function.
It alsoo allows passing additional parameters to the matching case function by using lua's
...
syntax.Error handling is already included using core functions like
ESX.IsFunctionReference
,ESX.AssertType
or lua'sassert
Usage Example
As an example, i took this piece of code you could use for a player manager resource (similiar to EasyAdmin) where you can ban/kick players. You could use one net-event for all this and just store the actual event functions in the caseTable
PR Checklist