Skip to content

The npColshape helper ​

The shim also defines npColshape, a small layer that works only through the native API. It behaves the same on Legacy and on Enhanced, and it is what the np_* resources use. It adds what the natives leave to you: one definition table with degrees and named entity groups, enter and exit handlers per shape, player ids, tracking of who is inside, clean-up, re-creation after a restart of np_colshape and debug outlines.

lua
local zone = npColshape.create({
  type = 'box', coords = vector3(441.0, -982.0, 30.7),
  size = vector3(8.0, 6.0, 4.0), rotation = 90.0,
  entityTypes = 'players',
})
npColshape.on(zone, {
  players = true,
  enter = function(ped, id, playerSource) end,
  exit = function(ped, id, playerSource) end,
})

Members ​

Member
npColshape.native true when the real natives exist (GTAV Enhanced)
npColshape.version LIB_VERSION of the shim (2)
npColshape.EntityType / npColshape.ShapeType The enums
npColshape.available() true when the natives exist, or on Legacy when np_colshape is started
npColshape.onReady(fn) Runs fn() now if available(). On Legacy again after every np_colshape (re)start. On Enhanced once
npColshape.create(def) id, or -1, reason
npColshape.on(id, opts) handle, or nil. opts may also be just the enter function
npColshape.off(handle) true if the handle was registered. Removes that handler pair
npColshape.isEntityIn(id, entity) boolean
npColshape.entitiesIn(id) Sorted list of entity handles
npColshape.describe(id) { type = 'sphere', x, y, z, radius, … } read back through the natives, or nil
npColshape.delete(id) DeleteColshape plus dropping the id's handlers and tracking. true, or false for a non-number
npColshape.debug(id | nil, on) Client: draws outlines. Server: returns false

npColshape.available() ​

true on Enhanced, and on Legacy while GetResourceState('np_colshape') == 'started'. Use it to pick between colshapes and your own fallback. See Adopting it in a resource.

npColshape.onReady(fn) ​

Runs fn() at once when colshapes are available. On Legacy every shape dies with np_colshape, so the helper runs fn again after every start of np_colshape (np_colshape:ready), once per start. On Enhanced it runs once. Create your shapes inside it.

lua
local stash
npColshape.onReady(function()
  stash = npColshape.create({
    type = 'sphere', coords = vector3(441.0, -982.0, 30.7), radius = 2.0,
    entityTypes = 'players', enter = function(ped) end,
  })
end)

An error in fn is caught and printed. It returns false if fn is not a function.

npColshape.create(def) ​

Field Used by Notes
type all 'sphere', 'circle', 'cylinder', 'cuboid', 'rectangle', 'polygon' (or 'poly'), 'box'
coords all but polygon vector3, { x =, y =, z = } or { x, y, z }. Circles need only x and y
radius sphere, circle, cylinder
height cylinder, cuboid, box Total height, centred on coords.z
width, depth or size cuboid, rectangle, box size = vector3(width, depth, height)
heading / rotation rectangle, box Degrees, converted to radians for the native. A vector3 rotation uses its z
points polygon List of vector2 / vector3 / { x =, y = }, or flat { x1, y1, … }. At least 3 points
minZ, maxZ polygon Missing means -10000 / 10000 (no limit in practice)
entityTypes all The allowed types; every other type is disabled. A number, an EntityType name, a group, or a list of these
dimension all, server Routing bucket, an integer. Ignored on the client
enter, exit, players, localPlayer, filter all Shorthand for npColshape.on(id, { … })

Entity groups ​

Group Types
'players' Player (11)
'peds' Ped (6), Player (11)
'npcs' Ped (6)
'vehicles' Automobile (0), Bike (1), Boat (2), Heli (4), Plane (9), Submarine (10), Trailer (12), Train (13)
'objects' Door (3), Object (5), GlassPane (14)
'pickups' Pickup (7), PickupPlacement (8)
lua
entityTypes = 'players'                    -- one group
entityTypes = { 'npcs', 'Automobile', 1 }  -- NPCs, cars and bikes

type = 'box' ​

It follows the PolyZone / ox_lib box:

  • without a height, it is a rectangle with that heading;
  • unrotated (heading a multiple of 360), it is a cuboid;
  • rotated with a height, it is the exact polygon of its four corners, with minZ / maxZ = z ∓ height / 2.

So a box from a PolyZone or ox_lib zone config can be moved over as it is.

Validation ​

The helper checks every field itself before it calls a native, so a bad definition never reaches a native on Enhanced. On failure it returns -1, reason:

Reason
bad_def def is not a table
bad_type Unknown type
bad_coords Missing or non-number coords (z is needed for every type but circle)
bad_radius, bad_height, bad_size Missing, non-number, or 0 or less
bad_heading heading / rotation is not a number or vector
bad_points Fewer than 3 points, an odd flat list, or a point without x and y
bad_z minZ / maxZ not a number, or minZ > maxZ
bad_entity_types An unknown name, a number outside 0 to 14, or an empty list
bad_dimension (server) not an integer
create_failed The native returned -1: on Legacy np_colshape is not started, the shape limit is reached or the polygon has too many points
lua
local id, reason = npColshape.create({
  type = 'cylinder', coords = coords, radius = 3.0,
})
if id == -1 then
  print('no zone: ' .. reason) -- bad_height
end

npColshape.on(id, opts) ​

Registers an enter / exit handler pair for one shape. The helper adds one handler pair for both events per resource and dispatches them by id, so you never compare shapeId yourself.

Option
enter(entity, id, player) / exit(entity, id, player) Errors are caught and printed
players Only player peds. Server: player is the player's source, mapped through GetPlayers + GetPlayerPed and cached for one tick. Client: player is the server id
localPlayer Client: only PlayerPedId(). On Legacy it can also scope the shape
filter(entity) Return true to accept. An error counts as false

A handler gets exit only for entities it got enter for. So exit still arrives after a player dropped or respawned, with the same player value as at enter.

lua
-- client: a prompt while the local player stands at the counter
npColshape.on(counter, {
  localPlayer = true,
  enter = function() showShopPrompt() end,
  exit = function() hideShopPrompt() end,
})

-- server: who is in the yard
npColshape.on(yard, {
  players = true,
  enter = function(ped, id, src) inYard[src] = true end,
  exit = function(ped, id, src) inYard[src] = nil end,
})

-- the function alone is the enter handler
local handle = npColshape.on(zone, function(entity) end)
npColshape.off(handle)

localPlayer scope (Legacy client) ​

When every handler on an id made with npColshape.create in this resource has localPlayer = true, the shim tells np_colshape (the setShapeScope export). From then on np_colshape tests only PlayerPedId() against that shape. Other players crossing it are never evaluated, and no onColshapeEnter / onColshapeExit is broadcast to every resource for them. Your handlers see the same events as before, since they only ever took the local player.

  • It is a Legacy optimization of the helper. On Enhanced the shim filters as it always did.
  • Only the creating resource knows the id, so nobody else is expected to listen to it. A raw AddEventHandler('onColshapeEnter', …) gets no events for other entities on a scoped id.
  • npColshape.on(id, …) without localPlayer lifts the scope, and npColshape.off of that handler sets it again. Entities already inside then enter on the next pass.
  • isEntityIn / entitiesIn on a scoped id know only the local player.
  • Shapes made with the raw natives are never scoped.

Shim and engine versions ​

Shim v2 and np_colshape 1.1.0 work with the older versions in both directions:

Shim np_colshape
v2 1.1.0 localPlayer scope is used
v2 1.0.x No setShapeScope export. The shim notices once per np_colshape start and keeps full evaluation
v1 1.1.0 The old shim never asks for a scope. Everything else of 1.1.0 (position-first passes, type cache, player-only peds) applies

So resources can update their bridge/np_colshape.lua one at a time.

Tracking ​

isEntityIn(id, entity) and entitiesIn(id) are kept from the events, for ids made with npColshape.create or passed to npColshape.on. Entities that were already inside before tracking started appear on their next enter. For a pure geometry question, use IsPointInsideColshape.

lua
if npColshape.isEntityIn(yard, GetPlayerPed(src)) then
  -- inside
end
for _, vehicle in ipairs(npColshape.entitiesIn(lot)) do end

npColshape.describe(id) ​

Reads the shape back through the Get*Data natives:

Shape Table
circle { type = 'circle', x, y, radius }
sphere { type = 'sphere', x, y, z, radius }
cylinder { type = 'cylinder', x, y, z, radius, height }
cuboid { type = 'cuboid', x, y, z, width, depth, height }
rectangle { type = 'rectangle', x, y, z, width, depth, heading } (heading in radians, as the native returns it)
polygon { type = 'polygon', points, minZ, maxZ } (points flat)

nil for an id that doesn't exist.

npColshape.debug(id | nil, on) ​

Client only. npColshape.debug(nil, true) draws the outlines of every shape your resource created with npColshape.create; npColshape.debug(id, true) draws one id, from any resource. on defaults to true; pass false to stop. Outlines are re-read every second, so deleted shapes stop being drawn. On the server it returns false. For every shape on a side, use /colshapes.

Lifecycle ​

  • Your resource stops. Shapes made with npColshape.create are deleted. On Legacy np_colshape would do it anyway; on Enhanced the helper makes sure of it.
  • np_colshape stops (Legacy). Every shape on that side dies with it, and ids start at 1 again. The helper drops its created shapes, handlers and tracking. When np_colshape is back, np_colshape:ready re-runs your onReady functions.
  • The shim is loaded twice (two shared_scripts entries). The second load returns at once.