fix(client/functions.lua): Closest Ped

Scripts like qb-drugs corner selling require finding the closest ped but include a list of peds to ignore, typically your own.
This commit is contained in:
Kakarot
2021-09-25 21:41:06 -06:00
parent 34c574daf2
commit 39b55b27fd
+37 -19
View File
@@ -100,10 +100,46 @@ end
-- Getters
function QBCore.Functions.GetVehicles() return GetGamePool('CVehicle') end
function QBCore.Functions.GetPeds() return GetGamePool('CPed') end
function QBCore.Functions.GetObjects() return GetGamePool('CObject') end
function QBCore.Functions.GetPlayers() return GetActivePlayers() end
function QBCore.Functions.GetPeds(ignoreList)
local pedPool = GetGamePool('CPed')
local ignoreList = ignoreList or {}
local peds = {}
for i = 1, #pedPool, 1 do
local found = false
for j=1, #ignoreList, 1 do
if ignoreList[j] == pedPool[i] then
found = true
end
end
if not found then
peds[#peds+1] = pedPool[i]
end
end
return peds
end
function QBCore.Functions.GetClosestPed(coords, ignoreList)
local ped = PlayerPedId()
if not coords then coords = GetEntityCoords(ped) end
local ignoreList = ignoreList or {}
local peds = QBCore.Functions.GetPeds(ignoreList)
local closestDistance = -1
local closestPed = -1
for i = 1, #peds, 1 do
local pedCoords = GetEntityCoords(peds[i])
local distance = #(pedCoords - coords)
if closestDistance == -1 or closestDistance > distance then
closestPed = peds[i]
closestDistance = distance
end
end
return closestPed, closestDistance
end
function QBCore.Functions.GetClosestPlayer(coords)
if coords == nil then
coords = GetEntityCoords(PlayerPedId())
@@ -175,24 +211,6 @@ function QBCore.Functions.GetClosestObject()
return closestObject, closestDistance
end
function QBCore.Functions.GetClosestPed()
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
local peds = GetGamePool('CPed')
local closestDistance = -1
local closestPed = -1
for i = 1, #peds, 1 do
local pedCoords = GetEntityCoords(peds[i])
local distance = #(pedCoords - coords)
if closestDistance == -1 or closestDistance > distance then
closestPed = peds[i]
closestDistance = distance
end
end
return closestPed, closestDistance
end
-- Vehicle
local function Round(value, numDecimalPlaces)