Get objects in radius of ped

Hey,

I’m looking for a function that returns all objects in a radius of the player ped.
I’m trying to make a sort of debug tool, so that I can view the hashkey of the object I am looking at to manipulate it in other scripts.

Anyone know of a way to do this?

Regards


I’ve already tried creating a list with all objects and loop through them, which follows I get 1 fps…

https://runtime.fivem.net/doc/reference.html#_0xfaa6cb5d
https://runtime.fivem.net/doc/reference.html#_0x4e129dbf
https://runtime.fivem.net/doc/reference.html#_0xdeda4e50

1 Like

Just delete the following, figured it out.

If you don’t mind me asking… what was it? I am trying to detect all objects of type within a specific radius and GetClosestObjectOfType only gets one objects and I want to get all?

Hi, I needed to know this too, did you find out how?

To get all the objects that exist on the client use : GetGamePool - Natives @ Cfx.re Docs.

In Lua :

local t  = GetGamePool('CObject') -- Table containing every objects handle

Hmm not much help I needed the dumps like 218085040, 666561306, -58485588, -206690185, 1511880420, 682791951 it seems to me they are on the server side because they didn’t show up in the list that GetGamePool(‘CObject’) gave me, but thanks

Any object that exist on the client will be returned by this function (unless said object is merged with the environment). Maybe you did something wrong will looking for those objects, if you mind sharing your code I can help you.

1 Like

Use GetEntityModel when you iterate over GetGamePool('CObject').

GetGamePool() returns the entity handles, so you need to get the model hash using GetEntityModel():

local objects = GetGamePool('CObject')

for _, entity in ipairs(objects) do
    local model = GetEntityModel(entity)
end

I’m trying to optimize this script [ESX] Onyx Dumpster Diving - #63 by happylowmandk and most of the optimization goes through GetClosestObjectOfType which I think gets all the coordinates of the map objects and then choose the closest one and my idea was to do the same but only with objects that are close to the player

ESX = nil

local searched = {3423423424}
local canSearch = true
local dumpsters = {218085040, 666561306, -58485588, -206690185, 1511880420, 682791951}
local searchTime = 14000

local dumpPos = {}

Citizen.CreateThread(function()
    while ESX == nil do
        TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
        Citizen.Wait(0)
    end
end)

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
        if canSearch then
            local ped = GetPlayerPed(-1)
            local pos = GetEntityCoords(ped)
            local dumpsterFound = false

            for i = 1, #dumpsters do
                local dumpster = GetClosestObjectOfType(pos.x, pos.y, pos.z, 1.0, dumpsters[i], false, false, false)
                local dumpPos = GetEntityCoords(dumpster)
                local dist = GetDistanceBetweenCoords(pos.x, pos.y, pos.z, dumpPos.x, dumpPos.y, dumpPos.z, true)

                if dist < 1.8 then
                    DrawText3Ds(dumpPos.x, dumpPos.y, dumpPos.z + 1.0, '[~g~H~w~] Procurar no lixo')
                    if IsControlJustReleased(0, 74) then
                        for i = 1, #searched do
                            if searched[i] == dumpster then
                                dumpsterFound = true
                            end
                            if i == #searched and dumpsterFound then
                                exports['mythic_notify']:DoHudText('error', 'This dumpster has already been searched')
                            elseif i == #searched and not dumpsterFound then
                                exports['mythic_notify']:DoHudText('inform', 'You begin to search the dumpster')
                                startSearching(searchTime, 'amb@prop_human_bum_bin@base', 'base', 'onyx:giveDumpsterReward')
                                TriggerServerEvent('onyx:startDumpsterTimer', dumpster)
                                table.insert(searched, dumpster)
                            end
                        end
                    end
                end
            end
        end
    end
end)

RegisterNetEvent('onyx:removeDumpster')
AddEventHandler('onyx:removeDumpster', function(object)
    for i = 1, #searched do
        if searched[i] == object then
            table.remove(searched, i)
        end
    end
end)

-- Functions

function startSearching(time, dict, anim, cb)
    local animDict = dict
    local animation = anim
    local ped = GetPlayerPed(-1)

    canSearch = false

    RequestAnimDict(animDict)
    while not HasAnimDictLoaded(animDict) do
        Citizen.Wait(0)
    end
    exports['progressBars']:startUI(time, "Searching Dumpster")
    TaskPlayAnim(ped, animDict, animation, 8.0, 8.0, time, 1, 1, 0, 0, 0)

    local ped = GetPlayerPed(-1)

    Wait(time)
    ClearPedTasks(ped)
    canSearch = true
    TriggerServerEvent(cb)
end

function DrawText3Ds(x, y, z, text)
    local onScreen,_x,_y=World3dToScreen2d(x,y,z)
  
    local scale = 0.5
  
    if onScreen then
        SetTextScale(scale, scale)
        SetTextFont(6)
        SetTextProportional(1)
        SetTextColour(255, 255, 255, 215)
        SetTextOutline()
        SetTextEntry("STRING")
        SetTextCentre(1)
        AddTextComponentString(text)
        DrawText(_x,_y)
    end
  end

I tried with this code however it didn’t return any results which I’m expecting would be local dumpsters = {218085040, 666561306, -58485588, -206690185, 1511880420, 682791951}

Citizen.CreateThread(function()
    local objects = GetGamePool('CObject')

    for _, entity in ipairs(objects) do
        local model = GetEntityModel(entity)

        if dumpsters[model] ~= nil then
            print(model)
        end
    end
end)

dumpsters[model] is looking for something at index 218085040 in the table which doesn’t exist. You need to iterate over dumpsters to check if the model is in there:

function inTable(tab, val)
    for index, value in ipairs(tab) do
        if value == val then
            return true
        end
    end

    return false
end

Citizen.CreateThread(function()
    local objects = GetGamePool('CObject')

    for _, entity in ipairs(objects) do
        local model = GetEntityModel(entity)

        if inTable(dumpsters, model) then
            print(model)
        end
    end
end)

ah of course my mistake, thank you however it seems that the code has not optimized anything which I want to think that GetClosestObjectOfType already does this process. Thank you anyway.

Replace GetDistanceBetweenCoords() with #(dumpPos - pos) and replace GetPlayerPed(-1) with PlayerPedId() - both are much faster.

Thank you it helped a little about 0.01 and 0.02 ms however it is still between 0.29 and 0.30 which is quite a lot, is it possible to go down more or am I just wasting time?

Letting the thread sleep if there’s no dumpsters around can help:

Citizen.CreateThread(function()
    while true do
        local letSleep = true
        
        if canSearch then
            local ped = PlayerPedId()
            local pos = GetEntityCoords(ped)
            local dumpsterFound = false

            for i = 1, #dumpsters do
                local dumpster = GetClosestObjectOfType(pos.x, pos.y, pos.z, 1.0, dumpsters[i], false, false, false)

                if dumpster then
                    letSleep = false -- dumpster found so prevent thread from sleeping
                    local dumpPos = GetEntityCoords(dumpster)
                    local dist = #(dumpPos - pos)

                    if dist < 1.8 then
                        DrawText3Ds(dumpPos.x, dumpPos.y, dumpPos.z + 1.0, '[~g~H~w~] Procurar no lixo')

                        if IsControlJustReleased(0, 74) then
                            --
                        end
                    end
                end
            end
        end

        -- No dumpsters in range so let the thread sleep
        if letSleep then
            Citizen.Wait(1000)
        end

        Citizen.Wait(0)
    end
end)
1 Like

You can create a lookup table for your dumpsters.

local dumpsters = {
    [218085040] = true, 
    [666561306] = true, 
    [-58485588] = true
}

CreateThread(function()
    while true do
        local objects = GetGamePool('CObject')

        for i = 1, #objects do
            local entity = objects[i]
            local model = GetEntityModel(entity)

            if dumpsters[model] then
                -- Distance computation here
            end
        end
        Wait(1000)
    end
end)
1 Like

And yeah, having two separate threads for distance computation and display or doing what Mobius01 said is a good idea.

@Elio @Mobius01
Thanks guys, I got it thanks to you, when I’m away from a dumpster it’s 0.00ms and when I get closer it’s 0.02ms very good and thank you both.

1 Like