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

PolyZone:CreateFromArray or PolyZone:CreateFromTable #62

Closed Rekington closed 2 years ago

Rekington commented 2 years ago

Looking to be able to create a number of different polyzones from an iterative list.

My objective is to basically use a defined polyzone, that I have put into the config. I then want to parse this into a table.

I then want to create a PolyZone from the table where each vector2 is basically vector2(v.vector2x, v.vector2y)

Maybe I am just doing this in a super complicated way. But I've created a config file to basically setup the whole script. Which is basically a territory script, if something happens within the PolyZone, e.g. sells drugs, do the thing. Simples.

Maybe I am just bad at LUA but I can do everything else that I need to do for this script, I just can't create PolyZones from the config, not sure how I would be able to do this.

    definedPolyzone = {
        [1] = {vector2x = 105.30870819092, vector2y = -3342.1044921875},
        [2] = {vector2x = 300.25759887695, vector2y = -3342.5158691406},
        [3] = {vector2x = 300.25439453125, vector2y = -3266.8269042969},
        [4] = {vector2x = 312.13751220703, vector2y = -3266.9416503906},
        [5] = {vector2x = 312.29177856445, vector2y = -2955.8986816406},
        [6] = {vector2x = 320.33383178711, vector2y = -2955.8371582031},
        [7] = {vector2x = 321.04968261719, vector2y = -2847.1987304688},
        [8] = {vector2x = 338.72348022461, vector2y = -2848.9453125},
        [9] = {vector2x = 377.4270324707, vector2y = -2813.26953125},
        [10] = {vector2x = 385.77325439453, vector2y = -2790.7465820312},
        [11] = {vector2x = 385.60702514648, vector2y = -2617.2426757812},
        [12] = {vector2x = 192.86708068848, vector2y = -2601.6726074219},
        [13] = {vector2x = 186.31010437012, vector2y = -2766.3989257812},
        [14] = {vector2x = 151.86489868164, vector2y = -2767.8151855469},
        [15] = {vector2x = 107.41565704346, vector2y = -2814.9001464844},
        [16] = {vector2x = 105.8104019165, vector2y = -2826.9538574219},
    },

I have also defined all the other things, once I've iterated through the list and gotten the table into the clientside, it creates it for the client, then it gets the remaining information:

    definedPolyzoneMinZ = 5.41330909729,
    definedPolyzoneMaxZ = 12.17768573761,
    definedPolyzoneName = "EastElysian",

Then it basically moves on to the next instance of territory in the config.

    local polyzoneCreation = PolyZone:Create({
    for o, p in pairs(v.definedPolyzone) do
    vector2(p.vector2x, p.vector2y)
    end
    },
    {name = v.definedPolyzoneName,
    minZ = v.definedPolyzoneMinZ,
    maxZ = v.definedPolyzoneMaxZ,
    })

Something like this, but maybe my syntax is wrong.

mkafrin commented 2 years ago

Yeah, your syntax is a bit off. Kinda an interesting python list comprehension pattern, but unfortunately doesn't exist in lua. Try something like this.

local points = {}
for o, p in ipairs(v.definedPolyzone) do
  points[#points+1] = vector2(p.vector2x, p.vector2y)
end
local polyzoneCreation = PolyZone:Create(points, {
  name = v.definedPolyzoneName,
  minZ = v.definedPolyzoneMinZ,
  maxZ = v.definedPolyzoneMaxZ,
})

Other than fixing the syntax, I changed pairs to ipairs, because pairs can go out of order which you wouldn't want when setting up your zone's points.

Also, on a separate note, for your definedPolyzone lists, you don't have to explicitly label each vector table by it's index. You can just omit that and it'll number them automatically. That prevents mistakes in the numbering or having to renumber things if you inserted a new point in the middle.

definedPolyzone = {
    {vector2x = 105.30870819092, vector2y = -3342.1044921875},
    {vector2x = 300.25759887695, vector2y = -3342.5158691406},
    {vector2x = 300.25439453125, vector2y = -3266.8269042969},
    {vector2x = 312.13751220703, vector2y = -3266.9416503906},
    {vector2x = 312.29177856445, vector2y = -2955.8986816406},
    {vector2x = 320.33383178711, vector2y = -2955.8371582031},
    {vector2x = 321.04968261719, vector2y = -2847.1987304688},
    {vector2x = 338.72348022461, vector2y = -2848.9453125},
    {vector2x = 377.4270324707, vector2y = -2813.26953125},
    {vector2x = 385.77325439453, vector2y = -2790.7465820312},
    {vector2x = 385.60702514648, vector2y = -2617.2426757812},
    {vector2x = 192.86708068848, vector2y = -2601.6726074219},
    {vector2x = 186.31010437012, vector2y = -2766.3989257812},
    {vector2x = 151.86489868164, vector2y = -2767.8151855469},
    {vector2x = 107.41565704346, vector2y = -2814.9001464844},
    {vector2x = 105.8104019165, vector2y = -2826.9538574219},
},
Rekington commented 2 years ago

Looks like I've learned something new.

Thanks a lot for the speedy reply. One of the best things I've ever used, amazing work.

I'll try out your updated syntax! Thank you again for all your help.

Regards

~ R

mkafrin commented 2 years ago

NP. I'll leave this up for 24 hours before closing. Feel free to reply if you have additional questions.