Add getElements with filtering support - #5188
Open
FileEX wants to merge 3 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR adds a new
getElementsfunction that allows retrieving multiple element types at once and filtering them based on various properties.Syntax
The function returns nil if the types table is empty. Otherwise, it returns a table organized by element types as keys—for example:
If the filter table for a specific element type is empty, all elements of that type are returned, behaving exactly as if you had used
getElementsByType.Filter table syntax
The first argument is a table containing the element types to search for and their respective filters.
Each element type can have its own filter table:
{ player = { streamedIn = true, onScreen = true, dimension = 0 }, vehicle = { model = {411, 415}, isUnoccupied = true } }The key represents the element type, while its value is a table containing the filters for that type.
Filters
An important aspect of the filtering system is that the presence of a key determines whether the filter is checked. For example:
onScreen = falsedoes not mean that the onScreen filter is disabled. Instead, it means that only elements that are not currently on screen will be returned. The same applies to all other boolean filters, such as streamedIn, isLowLOD, isUnoccupied, etc. If a filter should not be checked, simply omit its key from the filter table.
Common filters
The following filters are available for multiple element types:
The following comparison operators are supported for alpha and health:
==,~=,<=,>=,<,>For example:
will only return players with 50 HP or less.
Type-specific filters
Player
Ped / Player
Vehicle
Object
engineGetModelPhysicalPropertiesGroup == -1Object / Building
Marker
Pickup
Blip
Colshape
Team
Weapon / Projectile
Current scope
For now, this function is client-side only.
The API implementation needs some feedback first, so I would like to make sure that the current approach and syntax are suitable before adding the server-side implementation.
Once the API implementation is considered ready, a server-side version of getElements will be added as well.
Motivation
#4797
Performance
The function was benchmarked using 1,000 consecutive calls with different numbers of elements:
The main overhead is expected to come from the Lua/C++ boundary, particularly parsing the Lua filter tables into C++ and converting the resulting elements back into Lua values.
These benchmarks use 1,000 consecutive calls, which is considerably more frequent than the expected usage in typical scripts. In normal use, for example calling
getElementsonce per second or periodically from a timer, the performance impact should be negligible.Test plan
Checklist