Skip to content

Commit 9b9b4b3

Browse files
committed
Move-refactor-entity-functions-esx_lib
1 parent 0f4b808 commit 9b9b4b3

File tree

3 files changed

+90
-83
lines changed

3 files changed

+90
-83
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
--All client-side functions outsourced from the Core to the lib will be stored here for compatability, e.g:
1+
--All client-side functions outsourced from the Core to the lib will be stored here for compatability, e.g:
2+
3+
ESX.Game.GetClosestEntity = xLib.entity.closest
4+
EnumerateEntitiesWithinDistance = xLib.entity.EnumerateWithinDistance
5+
ESX.Game.Teleport = xLib.entity.Teleport

[core]/es_extended/client/functions.lua

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -473,26 +473,6 @@ function ESX.Game.GetPedMugshot(ped, transparent)
473473
return mugshot, GetPedheadshotTxdString(mugshot)
474474
end
475475

476-
---@param entity integer The entity to get the coords of
477-
---@param coords table | vector3 | vector4 The coords to teleport the entity to
478-
---@param cb? function The callback function
479-
function ESX.Game.Teleport(entity, coords, cb)
480-
481-
if DoesEntityExist(entity) then
482-
RequestCollisionAtCoord(coords.x, coords.y, coords.z)
483-
while not HasCollisionLoadedAroundEntity(entity) do
484-
Wait(0)
485-
end
486-
487-
SetEntityCoords(entity, coords.x, coords.y, coords.z, false, false, false, false)
488-
SetEntityHeading(entity, coords.w or coords.heading or 0.0)
489-
end
490-
491-
if cb then
492-
cb()
493-
end
494-
end
495-
496476
---@param object integer | string The object to spawn
497477
---@param coords table | vector3 The coords to spawn the object at
498478
---@param cb? function The callback function
@@ -689,32 +669,6 @@ function ESX.Game.GetClosestVehicle(coords, modelFilter)
689669
return ESX.Game.GetClosestEntity(ESX.Game.GetVehicles(), false, coords, modelFilter)
690670
end
691671

692-
---@param entities table The entities to search through
693-
---@param isPlayerEntities boolean Whether the entities are players
694-
---@param coords table | vector3 The coords to search from
695-
---@param maxDistance number The max distance to search within
696-
---@return table
697-
local function EnumerateEntitiesWithinDistance(entities, isPlayerEntities, coords, maxDistance)
698-
local nearbyEntities = {}
699-
700-
if coords then
701-
coords = vector3(coords.x, coords.y, coords.z)
702-
else
703-
local playerPed = ESX.PlayerData.ped
704-
coords = GetEntityCoords(playerPed)
705-
end
706-
707-
for k, entity in pairs(entities) do
708-
local distance = #(coords - GetEntityCoords(entity))
709-
710-
if distance <= maxDistance then
711-
nearbyEntities[#nearbyEntities + 1] = isPlayerEntities and k or entity
712-
end
713-
end
714-
715-
return nearbyEntities
716-
end
717-
718672
---@param coords table | vector3 The coords to search from
719673
---@param maxDistance number The max distance to search within
720674
---@return table
@@ -757,42 +711,6 @@ function ESX.Game.RaycastScreen(depth, ...)
757711
return target, ESX.Game.GetShapeTestResultSync(StartShapeTestLosProbe(origin.x, origin.y, origin.z, target.x, target.y, target.z, ...))
758712
end
759713

760-
---@param entities table The entities to search through
761-
---@param isPlayerEntities boolean Whether the entities are players
762-
---@param coords? table | vector3 The coords to search from
763-
---@param modelFilter? table The model filter
764-
---@return integer, integer
765-
function ESX.Game.GetClosestEntity(entities, isPlayerEntities, coords, modelFilter)
766-
local closestEntity, closestEntityDistance, filteredEntities = -1, -1, nil
767-
768-
if coords then
769-
coords = vector3(coords.x, coords.y, coords.z)
770-
else
771-
local playerPed = ESX.PlayerData.ped
772-
coords = GetEntityCoords(playerPed)
773-
end
774-
775-
if modelFilter then
776-
filteredEntities = {}
777-
778-
for currentEntityIndex = 1, #entities do
779-
if modelFilter[GetEntityModel(entities[currentEntityIndex])] then
780-
filteredEntities[#filteredEntities + 1] = entities[currentEntityIndex]
781-
end
782-
end
783-
end
784-
785-
for k, entity in pairs(filteredEntities or entities) do
786-
local distance = #(coords - GetEntityCoords(entity))
787-
788-
if closestEntityDistance == -1 or distance < closestEntityDistance then
789-
closestEntity, closestEntityDistance = isPlayerEntities and k or entity, distance
790-
end
791-
end
792-
793-
return closestEntity, closestEntityDistance
794-
end
795-
796714
---@return integer | nil, vector3 | nil
797715
function ESX.Game.GetVehicleInDirection()
798716
local _, hit, coords, _, _, entity = ESX.Game.RaycastScreen(5, 10, ESX.PlayerData.ped)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
xLib.entity = {}
2+
3+
---@param entities table The entities to search through
4+
---@param isPlayerEntities boolean Whether the entities are players
5+
---@param coords? table | vector3 The coords to search from
6+
---@param modelFilter? table The model filter
7+
---@return integer, integer
8+
function xLib.entity.closest(entities, isPlayerEntities, coords, modelFilter)
9+
local closestEntity, closestEntityDistance, filteredEntities = -1, -1, nil
10+
11+
if coords then
12+
coords = vector3(coords.x, coords.y, coords.z)
13+
else
14+
local playerPed = PlayerPedId()
15+
coords = GetEntityCoords(playerPed)
16+
end
17+
18+
if modelFilter then
19+
filteredEntities = {}
20+
21+
for currentEntityIndex = 1, #entities do
22+
if modelFilter[GetEntityModel(entities[currentEntityIndex])] then
23+
filteredEntities[#filteredEntities + 1] = entities[currentEntityIndex]
24+
end
25+
end
26+
end
27+
28+
for k, entity in pairs(filteredEntities or entities) do
29+
local distance = #(coords - GetEntityCoords(entity))
30+
31+
if closestEntityDistance == -1 or distance < closestEntityDistance then
32+
closestEntity, closestEntityDistance = isPlayerEntities and k or entity, distance
33+
end
34+
end
35+
36+
return closestEntity, closestEntityDistance
37+
end
38+
39+
---@param entities table The entities to search through
40+
---@param isPlayerEntities boolean Whether the entities are players
41+
---@param coords? table | vector3 The coords to search from
42+
---@param maxDistance number The maximum distance
43+
---@return table
44+
function xLib.entity.EnumerateWithinDistance(entities, isPlayerEntities, coords, maxDistance)
45+
local nearbyEntities = {}
46+
47+
if coords then
48+
coords = vector3(coords.x, coords.y, coords.z)
49+
else
50+
local playerPed = PlayerPedId()
51+
coords = GetEntityCoords(playerPed)
52+
end
53+
54+
for k, entity in pairs(entities) do
55+
local distance = #(coords - GetEntityCoords(entity))
56+
57+
if distance <= maxDistance then
58+
nearbyEntities[#nearbyEntities + 1] = isPlayerEntities and k or entity
59+
end
60+
end
61+
62+
return nearbyEntities
63+
end
64+
65+
---@param entity integer The entity to get the coords of
66+
---@param coords table | vector3 | vector4 The coords to teleport the entity to
67+
---@param cb? function The callback function
68+
function xLib.entity.Teleport(entity, coords, cb)
69+
70+
if DoesEntityExist(entity) then
71+
RequestCollisionAtCoord(coords.x, coords.y, coords.z)
72+
while not HasCollisionLoadedAroundEntity(entity) do
73+
Wait(0)
74+
end
75+
76+
SetEntityCoords(entity, coords.x, coords.y, coords.z, false, false, false, false)
77+
SetEntityHeading(entity, coords.w or coords.heading or 0.0)
78+
end
79+
80+
if cb then
81+
cb()
82+
end
83+
end
84+
85+
return xLib.entity

0 commit comments

Comments
 (0)