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
204 stars 191 forks source link

Not an issue, more an question.. #28

Closed RobijnGit closed 4 years ago

RobijnGit commented 4 years ago

Hey, i've read the Wiki of this repo, but i can't find out what i want..

Is there an way to create an polyzone and when the player enters or leaves the zone it only executes the code, instead of looping the code when the player is in the zone?

Thnx.

mkafrin commented 4 years ago

Hey @RobijnGit, you can find this in the second code example here. The onPlayerInOut helper function takes a callback function with two parameters: whether the player is inside or outside the zone and the point they were at. This callback will only be triggered when they enter or leave the zone.

RobijnGit commented 4 years ago

@mkafrin Thanks for youre response, i've tested a bit around with it and im getting this error:

Error loading script client/cl_main.lua in resource nor-entitymovement: @nor-entitymovement/client/cl_main.lua:48: attempt to index a nil value (local 'PoliceZone')

This is my fxmanifest.lua

client_scripts {
    'config.lua',
    'client/cl_main.lua',
    '@PolyZone/client.lua',
    '@PolyZone/BoxZone.lua',
    '@PolyZone/CircleZone.lua',
    '@PolyZone/ComboZone.lua',
    '@PolyZone/EntityZone.lua',
}

server_scripts {
    'config.lua',
    'server/sv_main.lua',
}

And here the client/cl_main.lua

local PoliceZone = nil
local entity = ''

Citizen.CreateThread(function()
    while PolyZone ~= nil do
        Citizen.Wait(1)
        PoliceZone = PolyZone:Create({
            vector2(408.63690185547, -1032.0905761719),
            vector2(408.45907592773, -1018.2272338867),
            vector2(417.61178588867, -1018.1384277344),
            vector2(417.671875, -1031.5482177734)
        }, {
            name = "police-entrance",
            minZ = 20.00,
            maxZ = 40.00,
            debugGrid = true,
            gridDivisions = 25
        })
        print('^4[nor-entitymovement] ^7Created new polyzone..')
        return
    end
end)

local insidePoliceZone = false
PoliceZone:onPlayerInOut(function(isPointInside, point)
    print(isPointInside)
end)

I am pretty sure that it giving me that error because PoliceZone is defined as nil and the polyzone inst created yet because its still retreiving all the PolyZone data from the other script.. Or am i just being dumb?

mkafrin commented 4 years ago

The PolyZone imports in your fxmanifest need to be above 'client/cl_main.lua'. Then you won't need the while loop in cl_main.lua. The reason that doesn't work currently is because you are calling PoliceZone:onPlayerInOut outside the while loop, so it's called before PoliceZone is ever set to anything. If you put the imports above 'client/cl_main.lua' then all the PolyZone scripts will be fully loaded before cl_main.lua is run. That way you can just do this:

local entity = ''
local PoliceZone = PolyZone:Create({
    vector2(408.63690185547, -1032.0905761719),
    vector2(408.45907592773, -1018.2272338867),
    vector2(417.61178588867, -1018.1384277344),
    vector2(417.671875, -1031.5482177734)
}, {
    name = "police-entrance",
    minZ = 20.00,
    maxZ = 40.00,
    debugGrid = true,
    gridDivisions = 25
})
print('^4[nor-entitymovement] ^7Created new polyzone..')

local insidePoliceZone = false
PoliceZone:onPlayerInOut(function(isPointInside, point)
    print(isPointInside)
end)

On a more general note, usually any resources you're importing with @ should be at the very top of your client_scripts, so that they are fully loaded before any of your client scripts run.

mkafrin commented 4 years ago

@RobijnGit Not sure if you get a notification for a regular reply, so @ing you to be sure.

RobijnGit commented 4 years ago

Dindt got the notification before, I've tested it and works correctly now! Thnx <3