qbcore-framework / qb-target

A better interaction system for FiveM
GNU General Public License v3.0
94 stars 240 forks source link

AddEntityZone always passes wrong entity 444418 [BUG] #123

Closed enc0ded closed 2 years ago

enc0ded commented 2 years ago

Describe the bug AddEntityZone always returns same entity.

To Reproduce Steps to reproduce the behavior:

            print('Passed Entity', entity) -- correct entity
            exports['qb-target']:AddEntityZone("test-"..entity, entity, {
                name = "test-"..entity,
                heading = GetEntityHeading(entity),
                debugPoly = true,
            }, {
                options = {
                    {
                        type = "client",
                        targeticon = "fas fa-test",
                        label = "Pick Up",
                        action = function(entity)
                            print('Entity from action', entity) -- always returns 444418 for every entity
                        end
                    },
                },
                distance = 2.5
              })

Expected behavior To return relative entity to client

enc0ded commented 2 years ago

I got around this by passing the netID instead but still not sure why it doesn't return the correct entity.

BerkieBb commented 2 years ago

You don't pass the entity yourself in the options, qb-target makes that variable for you

enc0ded commented 2 years ago

I've updated my post to reflect that. It still returns the same entity for each seperate object.

TheiLLeniumStudios commented 2 years ago

@enc0ded you might be hitting into variable shadowing issue due to the scope. Try changing the action function to

action = function(entity2)
    print("Entity from action", entity2)
end
BerkieBb commented 2 years ago
CreateThread(function()
    local coords = GetEntityCoords(PlayerPedId())
    RequestModel(`a_m_m_indian_01`)
    local entity = CreatePed(0, `a_m_m_indian_01`, coords.x, coords.y, coords.z, 0, true, true)
    print('Passed Entity', entity)
    exports['qb-target']:AddEntityZone("test-"..entity, entity, {
        name = "test-"..entity,
        heading = GetEntityHeading(entity),
        debugPoly = true,
    }, {
        options = {
            {
                label = "Pick Up",
                icon = "fas fa-circle",
                action = function(ent)
                    print('Entity from action', ent)
                end
            },
        },
        distance = 2.5
    })
end)

I put this code in a client.lua of my testing resource and this works fine, the entity handle is correct each time

BerkieBb commented 2 years ago

So I think your issue was what TheiLLeniumStudios mentioned