Skip to content

Events, state bags & commands ​

Client events ​

client

Local events (AddEventHandler), fired on each client:

Event Arguments Fired when
np_npchandler:ready { enabled, epoch } np_npchandler's first definition snapshot arrived on this client. Again after every restart of np_npchandler. See Ready
np_npchandler:spawned id, entity, data A local NPC was created on this client, or a networked NPC streamed in. data is the NPC's data table
np_npchandler:despawned id, entity A local NPC is about to be deleted, or a networked NPC streamed out. Raised before deletion, so the handle is still valid
np_npchandler:interact id, entity, data, option After any NPC's interact option was used, once its own event has fired. option is the option's name
np_npchandler:pedReady handle, entity, netId, reason An immediate ped was built (entity = 0: failed, see reason)
lua
AddEventHandler('np_npchandler:spawned', function(id, entity, data)
  if id:find('^np_shops:clerk_') then
    clerks[id] = entity
  end
end)

AddEventHandler('np_npchandler:pedReady', function(handle, entity, netId, reason)
  if handle == myDogHandle and entity == 0 then
    print('k9 failed: ' .. reason)
  end
end)
  • spawned, despawned and interact fire for every resource's NPCs. Filter by your id prefix. pedReady fires for every resource's immediate peds; compare the handle.
  • Your own interact events are fired with (id, entity, data, receipt, option) on the client, or (id, netId, data, receipt, option) on the server with server = true.
  • Do not delete the entity. Remove or disable the NPC instead.

Server events ​

server

Event Arguments Fired when
np_npchandler:ready { enabled, epoch } np_npchandler has started and registered its own definitions. Register your NPCs again here after a restart. See Ready
np_npchandler:definitionRegistered { id, owner, version, mode } An NPC was registered
np_npchandler:incident incident A reaction escalated. The v0.2.0 payload, see Incidents
lua
AddEventHandler('np_npchandler:incident', function(incident)
  print(('[npc] %s: %s by %d (severity %d)'):format(incident.npcId, incident.category,
    incident.subjectPlayer, incident.severity))
end)

Ready ​

np_npchandler:ready fires on the server when the handler started and on the client once its first definition snapshot arrived. Both fire again after a restart of np_npchandler, which drops every registration it held. The payload is { enabled, epoch }, with epoch the server's registry epoch. Register in a handler and once on your own start:

lua
local function register() exports.np_npchandler:addLocalNpc({ ... }) end
AddEventHandler('np_npchandler:ready', register)
if GetResourceState('np_npchandler') == 'started' then register() end -- 'id_in_use' when both ran is harmless

See Installation.

Net events ​

These are internal. Listed so you know what they are; don't trigger them yourself, except through the exports. np_npchandler:dialogueCue may be listened to on the client.

Event Direction Arguments Purpose
np_npchandler:snapshotRequest client → server Asks for a full snapshot. See Sync
np_npchandler:snapshot server → client payload All NPC definitions and server population overrides
np_npchandler:state server → all clients changes One change to definitions or population, with its revision
np_npchandler:bucket server → client bucket The player's routing bucket
np_npchandler:npcState server → clients in scope state Runtime state of a networked NPC: net id, generation, lifecycle, behaviour
np_npchandler:dialogueCue server → clients in scope { npcId, netId, generation, cueId, behavior } A dialogue cue to play. Unchanged from v0.2.0
np_npchandler:interactionRequest client → server request A reaction request
np_npchandler:interactionCancel client → server { requestId, npcId, generation } Cancels a pending request
np_npchandler:interactionOutcome server → client { requestId, npcId, accepted, reason, … } The answer to a request. Accepted ones add decision, cooldownMs, reactionId, incidentEmitted, data
np_npchandler:controlRequest client → server { netId, action, args } Runtime control for a ped this client doesn't control
np_npchandler:control server → owning client { netId, action, args, from } The checked request, routed to the client that owns the ped
np_npchandler:clientPedTicketRequest / :clientPedTicket both requestId[, ticket, reason] Single-use creation ticket for a client-created networked ped
np_npchandler:clientPedCreated / :clientPedAck both { netId, ticket, spec } / netId, ok, reason Registration with the ticket; the server retries about 3 s until the entity arrives, then acknowledges
np_npchandler:clientPedDelete / :clientPedGone both netId, delayMs / netId Deletion by the server (optionally delayed); the creator is told when the ped is gone
np_npchandler:debug server → client summary Toggles the client debug print. Sent only after the permission check

Sync ​

  • A client asks for a snapshot when it starts. Every later change to definitions or server population overrides is one state message with a consecutive revision. A client that sees a gap asks for a new snapshot.
  • The server answers at most sync.maxSnapshotsPerPlayer (3) requests per sync.snapshotWindowMs (10 s) per player. A client retries after the window plus 5 s.
  • Snapshots with more than sync.latentSnapshotThreshold (64) NPCs are sent as a latent event. State messages that arrive before the snapshot are held and applied on top of it.
  • Each start of np_npchandler has a new epoch, so clients drop state from a previous run.
  • Runtime state of networked NPCs is not on this stream. It goes only to players in scope (npcState).

State bags ​

Entity state bags, all written by the server except np_npchandler:applied:

Bag On Value Purpose
np_npchandler:npc Networked NPCs { id, definitionVersion, generation } Clients match a net id to the NPC only when this bag matches, so a reused net id never gets the wrong ped tasked
np_npchandler:spec Immediate networked peds The public definition without coords Look, flags, behaviour and held props. The controlling client applies it; every client renders the held props
np_npchandler:applied Networked peds A marker Written by the client that applied the full appearance, so a later owner after a migration only re-applies flags

The server guards np_npchandler:npc and np_npchandler:spec: a value a client writes is replaced with the server's value (or cleared on entities the handler does not know). Clients re-validate every spec they read.

Commands ​

Command Who What it does
npchandler_debug Server console Prints definitions by mode, networked peds spawned / dormant / failed, immediate peds (server / client) and population overrides
/npchandler_debug Players allowed np_npchandler.debug Toggles a print of the client counts to F8 every 2 seconds, and prints a server summary once. Others get no answer

The permission is checked with np_admin's npAdmin.can(source, 'np_npchandler.debug'). The node is registered with np_admin as NPC handler debug, so staff grant it in np_admin's permissions panel. Without np_admin, the vendored lib allows the server console, identifiers listed in the np_admin_fallback convar, and on ESX the groups in np_admin_fallback_groups (default admin,superadmin). See np_admin → Without np_admin.