mkafrin / PolyZone

PolyZone is a FiveM mod to define zones of different shapes and test whether a point is inside or outside of the zone
MIT License
202 stars 193 forks source link

Adding Zones #40

Closed cademullen closed 3 years ago

cademullen commented 3 years ago

Could I get some examples on ComboZone AddZone().

cademullen commented 3 years ago

function createHouseZones(points) for k, v in pairs(points) do local _coords, isInsidePrevious = vector3(v["Coords"].enter.x, v["Coords"].enter.y, v["Coords"].enter.z), false

        local newzone = #zones + 1

        zones[newzone] = BoxZone:Create(_coords, 1.5, 1.5,  {
            name = ('houseZone-%s'):format(newzone), data = { v }, useZ = true, maxZ = v["Coords"].enter.z + 1.5, minZ = v["Coords"].enter.z - 1.5 , heading = v["Coords"].enter.h, debugPoly = true
        })

end

comboZone = ComboZone:Create(zones, { name = 'houseZones' })

comboZone:onPlayerInOutExhaustive(function(isPointInside, point, insideZones, enterZones, leftZones)

    if enterZones then
        for i=1, #enterZones do
            print(tprint(enterZones[i].data))
            createPlayerInteractions(i, enterZones[i].data)
            isInAZone = true

            inCurrentZone = enterZones[i]
            break;
        end
    end

    if leftZones then
        for i=1, #leftZones do
            isInAZone = false
        end
    end
end)

end

Im trying to get the player interaction for each zone they enter. I do have this when a new zone gets created

function addHouse(point)

    local newzone = #zones + 1

    zones[newzone] = BoxZone:Create(_coords, 1.5, 1.5,  {
        name = ('houseZone-%s'):format(newzone), data = { point }, useZ = true, maxZ = point["Coords"].enter.z + 1.5, minZ = point["Coords"].enter.z - 1.5 , heading = point["Coords"].enter.h, debugPoly = true
    })

    ComboZone:AddZone(inCurrentZone)

end

mkafrin commented 3 years ago

AddZone adds a new zone to the ComboZone. You don't need a separate zones table if you use AddZone since you can just pass the zones into it directly. See the below update of your code.

function createHouseZones(points)
    comboZone = ComboZone:Create({}, {name = "houseZones"})
    for k, v in pairs(points) do
        local _coords, isInsidePrevious = vector3(v["Coords"].enter.x, v["Coords"].enter.y, v["Coords"].enter.z), false

        comboZone:AddZone(BoxZone:Create(_coords, 1.5, 1.5, {
            name = ("houseZone-%s"):format(newzone),
            data = {v},
            useZ = true,
            maxZ = v["Coords"].enter.z + 1.5,
            minZ = v["Coords"].enter.z - 1.5,
            heading = v["Coords"].enter.h,
            debugPoly = true
        }))
    end

    comboZone:onPlayerInOutExhaustive(
        function(isPointInside, point, insideZones, enterZones, leftZones)
            if enterZones then
                for i = 1, #enterZones do
                    print(tprint(enterZones[i].data))
                    createPlayerInteractions(i, enterZones[i].data)
                    isInAZone = true

                    inCurrentZone = enterZones[i]
                    break
                end
            end

            if leftZones then
                for i = 1, #leftZones do
                    isInAZone = false
                end
            end
        end
    )
end

function addHouse(point)
    local _coords = vector3(v["Coords"].enter.x, v["Coords"].enter.y, v["Coords"].enter.z)
    local newZone = BoxZone:Create(_coords, 1.5, 1.5, {
    name = ("houseZone-%s"):format(newzone),
        data = {point},
        useZ = true,
        maxZ = point["Coords"].enter.z + 1.5,
        minZ = point["Coords"].enter.z - 1.5,
        heading = point["Coords"].enter.h,
        debugPoly = true
    })
    ComboZone:AddZone(newZone)
end
cademullen commented 3 years ago

Ahh that makes sense, but I do get this error.

SCRIPT ERROR: @polyzone/ComboZone.lua:172: attempt to get length of a nil value (local 'zones')

mkafrin commented 3 years ago

Oh in addHouse

ComboZone:AddZone(newZone)

should instead be

comboZone:AddZone(newZone)
cademullen commented 3 years ago

Yeah that was it appreciate the help. Do you have a discord that I could join, so if there are just some small questions I could contact you?

mkafrin commented 3 years ago

Nah sorry, no Polyzone discord. Not nearly enough traffic to justify it. Feel free to just make an issue though, after checking the wiki.