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

perf/refactor(client/functions): Refactor ESX.Game.GetClosestEntity for performance #1326

Closed
wants to merge 2 commits into from
Closed

perf/refactor(client/functions): Refactor ESX.Game.GetClosestEntity for performance #1326

wants to merge 2 commits into from

Conversation

itsmaty
Copy link
Contributor

@itsmaty itsmaty commented Feb 28, 2024

Refactored the ESX.Game.GetClosestEntity, instead of using two seperate k, v loops for filtering and distance checking i combined the logic into one faster for loop

also the coords parameter is either nil or an vector3 so if set, there is no need to parse it to an vector 3

i am not sure what the isPlayerEntities parameter is for and if true returning the index from the entities table (line 522), why?
i left that specific logic as is to not break anything

if coords are set, its already an vector3 because its the only way to display coords in one param
refactored the logic to iterate trough the entitys to improve performance

combined filtering and distance checking both ok, v loops into one single for loop
@itsmaty
Copy link
Contributor Author

itsmaty commented Feb 28, 2024

so i made a mistake here
since the isPlayerEntities parameter is only used for ESX.Game.GetClosestPlayer because there ESX.Game.GetPlayers is called like this ESX.Game.GetPlayers(true, true) (See Line 458) and that does that the ESX.Game.GetPlayers` function returns a key value table with the player as the key and the ped as the value

this gets passed into ESX.Game.GetClosestEntity withe the isPlayerEntities = true and the ok, v loop would be indeed needed here because the loop schould return the player (the key of the table) and with an for loop we can only acess the value that what this line is for i woundered about
closestEntity, closestEntityDistance = isPlayerEntities and k or entity, distance in my refactor i replaced the k variable with the current loop index which is wrong.

I will do another commit, removing the logic for isPlayerEntities in ESX.Game.GetClosestEntity and calling ESX.Game.GetPlayers(true) so it will return an array containing the players. then the for loop in ESX.Game.GetClosestEntity will work an correctly return the player.

@itsmaty itsmaty closed this Feb 28, 2024
@itsmaty
Copy link
Contributor Author

itsmaty commented Feb 28, 2024

ok nevermind you need the ped for distance but it should return the player so you need the player, ped k, v table
ok nothing i did made sende
just ignore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

1 participant