Skip to content

np_colshape v1.1.0 ​

FiveM for GTAV Enhanced ships colshape natives: CreateColshapeSphere, onColshapeEnter / onColshapeExit, entity-type filters and dimensions, all handled by the runtime. FiveM Legacy has none of them. np_colshape brings the same API to Legacy, with the same names, arguments, return values and events, so a resource written against the natives runs unchanged on both. On Enhanced it switches itself off.

There is no database, no NUI and no framework. np_admin is optional.

Two parts ​

Part What it is Who loads it
np_colshape resource The Legacy engine: shape pools, detection loops, events ensure np_colshape
lib/colshape.lua The shim: defines the natives on Legacy, plus the npColshape helper Every resource that uses colshapes, vendored byte-identical as bridge/np_colshape.lua

A resource never lists np_colshape in its dependencies and never includes @np_colshape/.... It loads its own copy of the shim. See Installation.

Legacy and Enhanced ​

The shim checks for the feature, not for a version. The natives count as present when pcall(function() return DoesColshapeExist(0) end) returns a boolean. On Legacy the global is missing or throws "native not found". The result is npColshape.native.

GTAV Enhanced FiveM Legacy
Colshape natives The real ones. The shim defines nothing Defined by the shim, forwarded to exports.np_colshape
onColshapeEnter / onColshapeExit Fired by the runtime Fired by np_colshape with TriggerEvent on the same side
np_colshape resource Idle. It logs once, and its exports forward to the natives The engine
npColshape helper Runs on top of the natives Runs on top of the shim, with the same behaviour

Nothing changes in your code when a server moves from Legacy to Enhanced. Keep the shim. ensure np_colshape can stay too.

lua
-- the same code on Legacy and on Enhanced
local stash = CreateColshapeSphere(441.0, -982.0, 30.7, 2.0)
SetColshapeEntityType(stash, 6, false) -- no NPCs

AddEventHandler('onColshapeEnter', function(entity, shapeId)
  if shapeId == stash then
    -- entity is inside
  end
end)

How it works on Legacy ​

  • Two pools. The client and the server each have their own shapes, ids and events. Nothing syncs between them, the same as with the natives. Ids are positive integers per side. They start at 1 and are never reused while np_colshape runs.
  • Broad phase. Shapes are indexed by their 2D bounding box into a grid of 64 m cells. Each entity is tested only against the shapes in its own cell. A shape that covers more than maxCellsPerShape cells, like a map-wide circle, sits on a short list that every entity is tested against (while one of them wants a type of that pool).
  • Entity pools. The server checks every OneSync entity (GetAllPeds, GetAllVehicles, GetAllObjects) and types it with GetNetTypeFromEntity. The client checks what it streams (GetGamePool CPed, CVehicle, CObject, CPickup). A pool is not read at all while no shape on that side wants any of its types.
  • Player-only peds. While no shape wants Ped, the ped pool is replaced by the player peds: GetPlayers + GetPlayerPed on the server, GetActivePlayers + GetPlayerPed on the client. A player's ped is Player, so this is the same set the pool gives after typing. The moment a shape wants Ped, the whole pool is read again.
  • Position first. Each entity costs one GetEntityCoords. It is typed, and on the server its routing bucket read, only when its cell, the wide list or its own record gives it a shape to test, and the bucket only for a shape that contains it. A handle just read from its pool is not checked with DoesEntityExist: a handle that is gone reads as the zero vector, and only then is its existence checked. On the server the position is read under a pcall, since a server native throws for a deleted entity (OneSync may delete one at any time).
  • Type cache. An entity's type is cached per pool and handle while the handle shows up in every pass of its pool. A handle that misses one pass is typed again.
  • Intervals. Peds and vehicles are checked every 100 ms on the client and every 200 ms on the server. Objects and pickups every 500 ms. A pass yields a frame after every sliceSize (200) entities, so it never blocks. With no shapes and nobody inside one, the loops wake once a second and do nothing. With adaptive rechecks on (off by default), non-player entities far from every shape are looked at less often.
  • State. Only entities that are inside at least one shape have a record. An entity is inside when its GetEntityCoords position is inside the shape, and the boundary counts.
  • Events. Each pass queues its events and fires them in one flush: every exit, then every enter. A handler that errors is caught and logged. It never stops the loop. A native that errors on one entity is logged and skips that entity only.
  • Ownership. The resource that created a shape (GetInvokingResource()) owns it. Its shapes are deleted when it stops, without events. Any resource may change or delete any shape, as with the natives.

On the client, shapes made with the helper whose handlers all take only the local player are evaluated against PlayerPedId() alone. See localPlayer scope.

The exact rules for enter, exit, typing and timing are on Behaviour & assumptions.

Performance ​

In FiveM the cost of a pass is mostly native calls (each crosses into C++) and allocations. tests/bench.lua runs the real loops against stubbed natives and counts them, in three worlds:

  • Client: 150 peds (20 players), 120 vehicles and 1500 objects streamed around the player; 300 small player-only shapes (120 of them nearby) and 5 polygon zones.
  • Server: 128 players, 1500 peds, 800 vehicles and 3000 objects; 480 small player-only shapes (30 in other buckets) and 20 gang-turf polygons of 100 points.
  • Mixed adds 20 (client) / 30 (server) shapes that want vehicles or every type.

Numbers are per pass of a loop group, over 200 passes, on Lua 5.5. "ms" is plain Lua with cheap stubs; in FiveM every native adds its own cost on top. Groups with no wanted pool are left out (0 natives either way).

Scenario Group Natives 1.0 → 1.1 ms 1.0 → 1.1 KB 1.0 → 1.1 Events 1.0 → 1.1
client, no shapes fast / slow 0 → 0 0.002 → 0.002 0.19 → 0 0
client, 305 shapes, nothing near fast 321 → 41 0.030 → 0.007 2.49 → 0.33 0
client, 305 player-only shapes fast 321 → 41 0.056 → 0.022 2.69 → 0.66 1.1 → 1.1
client, 305 shapes with localPlayer scope fast 321 → 2 0.052 → 0.003 2.69 → 0.13 1.1 → 0.3
client, mixed fast 812 → 272 0.164 → 0.103 3.93 → 3.90 1.4 → 1.4
client, mixed slow 3002 → 1502 0.705 → 0.484 18.29 → 18.10 0
server, no shapes fast / slow 0 → 0 0.002 → 0.002 0.19 → 0 0
server, 500 shapes, nothing near fast 3386 → 257 0.318 → 0.042 18.25 → 1.20 0
server, 500 player-only shapes fast 3428 → 275 0.501 → 0.174 19.70 → 2.82 7.1 → 7.1
server, mixed fast 7358 → 2449 1.295 → 0.757 28.77 → 28.82 7.2 → 7.2
server, mixed slow 9020 → 3001 1.340 → 0.733 36.24 → 36.05 0
server, mixed, adaptive = true fast 7358 → 1148 1.291 → 0.787 28.77 → 28.76 7.2 → 7.2
server, mixed, adaptive = true slow 9020 → 1672 1.341 → 1.133 36.24 → 36.06 0

What is left is one GetEntityCoords per entity of a wanted pool, plus the pool list itself (most of the KB: the pool natives return a fresh table). The localPlayer scope row also has fewer events, because other players crossing those shapes are no longer broadcast. These are stub counts, not in-game measurements.

sh
lua tests/bench.lua . [passes] [scenario filter]

Used by ​

These np_* resources vendor the shim and use np_colshape when it is available, with their own distance checks as the fallback: np_banking, np_cctv, np_cellnet, np_clothing, np_darts, np_drugs, np_environmental, np_farming, np_fishing, np_fuel, np_gangs, np_mechanic, np_prison, np_shops, np_surveillance, np_vending. None of them requires it. What each one moves onto colshapes is listed in Adopting it in a resource.

Status ​

Not yet run on a live server

Version 1.1.0 was written against the documented natives and stubbed natives. The plain Lua specs in tests/ pass (geometry of every shape, the grid, the native return values for valid and invalid ids, server and client detection, the debug overlay, the idle mode on Enhanced, the shim, the helper on a Legacy and a fake Enhanced runtime, the np_admin permission gate and settings, the 1.1 fast paths, and randomized worlds checked against brute force after every tick). Nothing has been checked in game yet: not the natives, not the events, not the performance with real player counts.

What needs a live test is listed on Behaviour & assumptions → Not verified in game.

Next steps ​