Adopting it in a resource ​
How to move a resource from its own distance loop to colshapes without making np_colshape a requirement. The np_* resources all follow this pattern.
1. Vendor the shim ​
Copy np_colshape/lib/colshape.lua byte-identical to bridge/np_colshape.lua in your resource and load it first:
-- fxmanifest.lua
shared_scripts {
'bridge/np_colshape.lua',
'shared/config.lua',
-- …
}
- No
dependencies { 'np_colshape' }, no@np_colshape/...include. See Installation → Not a dependency. - Keep the copy byte-identical so it can be compared and replaced with a newer
LIB_VERSIONin one step. Do not edit it in place. - The file is Lua 5.1 compatible on purpose, so it passes in resources linted with selene's
lua51std. - Loading it twice is harmless: the second load returns at once.
2. Keep the old loop as the fallback ​
While servers move over, a server may run Legacy without np_colshape. Pick at runtime:
if npColshape.available() then
npColshape.onReady(createZones)
else
startLegacyLoop()
end
available() is true on Enhanced (the natives are built in) and on Legacy while np_colshape is started.
onReady runs createZones now and again after every restart of np_colshape, because on Legacy
every shape dies with it.
A sturdier version keeps the distance check running and lets the colshape decide only while it can:
local colshapes = false
npColshape.onReady(function()
colshapes = true
createZones() -- rebuild every shape: ids start at 1 again after a restart
end)
AddEventHandler('onResourceStop', function(name)
-- Legacy: its shapes died with it, the distance check takes over again
if name == 'np_colshape' and not npColshape.native then
colshapes = false
end
end)
3. Migrate a distance loop ​
A typical points loop measures the player against every point a few times a second:
-- before
CreateThread(function()
while true do
local coords = GetEntityCoords(PlayerPedId())
for id, p in pairs(points) do
local near = #(coords - p.coords) <= p.radius
if near ~= (inside[id] == true) then
inside[id] = near or nil
if near then onEnter(p) else onExit(p) end
end
end
Wait(500)
end
end)
With colshapes, every point gets a sphere for the local player, and the events drive enter and exit:
-- after
local shapes = {}
local function addShape(id, p)
local shape = npColshape.create({
type = 'sphere', coords = p.coords, radius = p.radius,
entityTypes = 'players', localPlayer = true,
enter = function() inside[id] = true; onEnter(p) end,
exit = function() inside[id] = nil; onExit(p) end,
})
if shape ~= -1 then shapes[id] = shape end
end
local function removePoint(id)
if shapes[id] then npColshape.delete(shapes[id]) end
shapes[id], points[id] = nil, nil
end
npColshape.onReady(function()
shapes = {}
for id, p in pairs(points) do addShape(id, p) end
end)
What the np_* resources learned doing this:
- Let the distance decide until the first event. np_colshape reports a player who is already inside on its first pass after the shape was created (up to 100 ms on the client, 200 ms on the server). Until a shape's first event, or for about a second, keep using the distance check, so a player who leaves before that pass still gets the exit.
- Points that move stay on the distance check. A colshape doesn't follow an entity. Keep measuring points that sit on a vehicle or a ped.
- Routing buckets. Shapes are in bucket
0unless you passdimension. On the server, either create the shape in the right bucket or keep the distance check for players in other buckets. - Prompts every frame stay in your loop. Colshapes only tell you who is inside. Drawing a prompt and reading a key still needs a frame loop, but only while the player is inside a shape.
- Server presence. For "who is in this area" on the server, use
players = true: the handler gets the player's source, andexitarrives even after the player dropped. Keep a set from the events instead of measuring every player. - Rebuild on config changes. When zones change at runtime,
npColshape.deletethe old ids and create new ones. Keep the shape of a point whose coords and radius did not change: deleting and re-creating it fires an exit and an enter for a player standing there on every sync. - Don't walk every point while shapes run. The distance loop only needs the points whose shape has not reported yet, and points without a shape (create failed). Everything else follows the events.
From PolyZone or ox_lib zones ​
| You have | Use |
|---|---|
BoxZone / lib.zones.box with heading, minZ, maxZ |
type = 'box', coords, size = vector3(length, width, maxZ - minZ), heading in degrees, coords.z in the middle. See box |
CircleZone / lib.zones.sphere |
type = 'sphere' (or 'circle' without Z limits) |
PolyZone / lib.zones.poly |
type = 'polygon', points, minZ, maxZ |
onPlayerInOut / inside callbacks |
npColshape.on with localPlayer = true (client) or players = true (server) |
isPointInside |
IsPointInsideColshape(id, x, y, z) |
debugPoly = true |
npColshape.debug(id, true) |
How the np_* resources use it ​
All of them vendor bridge/np_colshape.lua, use colshapes while npColshape.available(), and run their distance
checks as before without np_colshape, while it is down or while a new shape settles. None of them lists it in
dependencies.
| Resource | What moves onto colshapes |
|---|---|
| np_banking | Enter / exit of its interaction points (ATMs, counters, vaults): one local-player sphere per point |
| np_cctv | Enter / exit of its interaction points: one local-player sphere per point |
| np_cellnet | Server: dead and weak signal zones as polygons (with their Z limits), jammers and Wi-Fi hotspots as spheres, players only. Every enter or exit re-evaluates that player's signal at once |
| np_clothing | Enter / exit of its store and wardrobe points: one local-player sphere per point |
| np_darts | Server: one sphere per pub and per board, players only, in the board's routing bucket. Pub presence and the match departure check read them |
| np_drugs | Enter / exit of its world points: one local-player sphere per point |
| np_environmental | Enter / exit of its interaction points, and the hazard zones of scenes, as local-player spheres |
| np_farming | Client: every world point as a local-player sphere. Server: while truffle walks run, the forest zones as 2D player circles |
| np_fishing | Enter / exit of its interaction points: one local-player sphere per point. Points that follow an entity keep the distance check |
| np_fuel | Enter / exit of its interaction points, and of its stations (one prompt-less point each): one local-player sphere per point |
| np_gangs | Turf polygons on the client (local player) and the server (who is in which turf), and its interaction points as local-player spheres |
| np_mechanic | The workshop zones and its interaction points: one local-player sphere each |
| np_prison | Client: world points as local-player spheres, and one sphere around each facility (its zone + 120 m). Server: the zones of alternative sentences, players only |
| np_shops | Enter / exit of its world points (counters, registers): one local-player sphere per point |
| np_surveillance | Enter / exit of its interaction points: one local-player sphere per point |
| np_vending | Enter / exit of the machine points: one local-player sphere per point |