Reactions & incidents ​
server
The perception, policy, dialogue cue and incident system of np_npchandler v0.2.0. It still works, and is now opt-in
per NPC: only networked NPCs with a perception field take part. NPCs without perception never enter this
path.
How a reaction runs ​
- A player comes within the NPC's
perception.radius(with line of sight whenlineOfSight = true). Their client sends one request. A resource can also send one withrequestInteraction(id, interactionId?). - The server checks the request: request id not replayed, rate limits, NPC enabled and active, definition version
and entity generation current, the interaction id allowed, NPC not busy or cooling down, the NPC entity and the
player alive and in the same bucket, and the distance (
radiusfor the perception interaction,interactionRadiusfor others). - Requests for one NPC are collected for
requests.arbitrationWindowMs(75 ms). The earliest valid one wins; the others getnpc_busy. - The interaction handler runs (if one is registered), then every policy of the NPC. The strongest decision
wins:
ignore<allow<warn<escalate. - The server replies to the player with the outcome, sends the decision's dialogue cue to players in scope, and
on
escalatefires a de-duplicated incident. - The NPC holds the reaction for
requests.reactionDurationMs(2.5 s), then cools down for the policy'scooldownMs.
Definition ​
local token, state = exports.np_npchandler:registerDefinition({
id = 'shop_24_7_clerk_01',
model = 'mp_m_shopkeep_01',
coords = { x = 24.0, y = -1346.0, z = 29.5, heading = 270.0 },
role = 'shop_clerk',
scenario = 'stand_impatient',
networked = true,
persistence = 'anchor',
perception = { radius = 12.0, interactionRadius = 2.0, lineOfSight = true, reactionCooldownMs = 8000 },
policies = { 'allow' },
dialogueSet = 'basic_staff',
interactionIds = { 'autonomous_perception' },
perceptionInteractionId = 'autonomous_perception',
})
| Field | Default | Notes |
|---|---|---|
perception.radius |
12.0 |
1 to 25 (definition.min/maxPerceptionRadius) |
perception.interactionRadius |
2.0 (at most radius) |
0.5 to 5, not above radius |
perception.lineOfSight |
true |
The client only sends a request with a clear line of sight |
perception.reactionCooldownMs |
8000 |
250 to 300000 |
policies |
{ 'allow' } |
Up to 8 policy ids |
interactionIds |
{ 'autonomous_perception' } |
Up to 16 interaction ids |
perceptionInteractionId |
the first of interactionIds |
Must be in interactionIds |
dialogueSet |
none | A key of Config.NpcHandler.dialogueSets |
Refusals: 'perception_requires_networked', 'perception_required' (reaction fields without perception),
'invalid_perception', 'invalid_perception_radius', 'invalid_interaction_radius', 'invalid_line_of_sight',
'invalid_reaction_cooldown', 'invalid_policies', 'invalid_interaction_ids',
'invalid_perception_interaction', 'dialogue_set_not_allowed', 'policy_dependency_unavailable' and
'interaction_handler_unavailable' (an owned policy or required handler is not registered by the NPC's owner), and
'interaction_handler_owned_elsewhere'.
perception works with registerNpc too, as long as the NPC is networked.
v0.2.0 compatible exports ​
These keep their v0.2.0 signatures. They use a token: the returned token must be passed to update or remove.
| Export | Returns | Notes |
|---|---|---|
registerDefinition(def, context?) |
token, state or nil, reason |
Networked and alwaysSpawned by default. context must be nil or {} ('registration_context_not_supported'). data may only contain letters, digits, spaces and . _ - |
updateDefinition(id, def, token) |
true, state or false, reason |
Refused while a reaction is running ('definition_in_use') |
removeDefinition(id, token) |
true or false, reason |
|
setNpcEnabled(id, enabled, token?) |
true or false, reason |
The token is required for NPCs from registerDefinition, not for registerNpc |
getNpcState(id) |
table or nil |
The v0.2.0 state shape plus mode, owner, lastError, spawnAttempts, lastPolicyResult, lastIncident |
registerPolicy(id, adapter) |
token or nil, reason |
|
updatePolicy(id, adapter, token) / removePolicy(id, token) |
true or false, reason |
|
registerInteractionHandler(id, adapter) |
token or nil, reason |
|
updateInteractionHandler(id, adapter, token) / removeInteractionHandler(id, token) |
true or false, reason |
The token exports refuse calls from np_npchandler itself ('invalid_invoking_resource'). Other refusals:
'duplicate_definition_id', 'invalid_definition_id', 'invalid_definition_shape', 'invalid_data_content',
'not_owner', 'definition_id_mismatch'.
The state table has id, definitionVersion, policyVersion, netId, generation, lifecycle, behavior,
targetPresent, cooldownRemainingMs, revision, mode, alwaysSpawned, enabled, modelHash, coords,
routingBucket, scenario, role, perception, interactionIds and perceptionInteractionId.
Adapters ​
Policies and interaction handlers are tables with one function. Both are called in pcall and fail closed: an
error or a bad result refuses the request.
-- server
local policyToken = exports.np_npchandler:registerPolicy('np_shops:staff_only_policy', {
assess = function(self, context)
-- context: { npc, player, interactionId, facts, now }
if context.facts.staffOnly then
return { decision = 'warn', cue = 'warning', cooldownMs = 10000 }
end
return { decision = 'allow' }
end,
})
local handlerToken = exports.np_npchandler:registerInteractionHandler('shop_staff_warning', {
handle = function(self, context)
-- context: { npc, player, interactionId, now }
return { eligible = true, facts = { staffOnly = true } }
end,
})
| Policy result field | Notes |
|---|---|
decision |
'ignore', 'allow', 'warn' or 'escalate' |
cue |
A cue id from the NPC's dialogue set. Without it, the set's cue for the decision is used |
cooldownMs |
Default: the NPC's reactionCooldownMs. Within definition.min/maxCooldownMs |
incidentCategory |
Required for escalate. Optional restriction: restrict.incidentCategories |
severity |
1 to incidents.maxSeverity (5), default 1 |
publicData |
Small table sent back to the requesting player: 3 levels, 16 entries, strings up to 128 characters, letters, digits, spaces and . _ - only |
An interaction handler returns { eligible = boolean, facts = table? }. eligible = false refuses the request
('ineligible'). The facts are passed to the policies.
- Policy ids are any safe id except the built-ins
allow,ignore,warnandescalate. - Interaction ids marked
handler = 'required'inallowedInteractionsneed a handler registered by the NPC's owner. - Adapters belong to the registering resource and are removed when it stops. Updating one while an NPC that uses it
is reacting, or removing one that any NPC still lists, is refused (
'adapter_in_use'). Other refusals:'invalid_adapter','adapter_id_not_allowed','duplicate_adapter_id','invalid_handler','not_owner'.
Dialogue cues ​
The server sends np_npchandler:dialogueCue to players in the NPC's scope and to the requester:
{ npcId = 'shop_24_7_clerk_01', netId = 123, generation = 4, cueId = 'warning', behavior = 'warning' }
np_npchandler plays it: the cue's speech line on every receiving client, the gesture only on the client that
controls the ped. Cue ids map to Config.NpcHandler.dialogueCues; nothing from the network selects a native name
directly. Dialogue audio resources can listen to the same event.
Incidents ​
On escalate, the server fires the local event np_npchandler:incident:
AddEventHandler('np_npchandler:incident', function(incident)
-- incident.incidentId, incident.incidentKey, incident.npcId, incident.npcGeneration,
-- incident.subjectPlayer, incident.subjectSourceGeneration, incident.category, incident.severity,
-- incident.policyVersion, incident.coords, incident.createdAt
if incident.category == 'shop_hostile' then
-- raise the robbery alarm
end
end)
Incidents are de-duplicated per NPC, generation, player, category and policy version within
incidents.cooldownMs (8 s, or the policy cooldown if longer), and rate-limited per player (3 per minute), per NPC
(10 per minute) and globally (100 per minute). The payload and dedup key are unchanged from v0.2.0.