Skip to content

Networked NPCs ​

server

A networked NPC (mode = 'networked') is one OneSync ped that every player sees in the same place. The server creates it while a player is near and deletes it when everybody has left. Only server registrations can be networked.

lua
exports.np_npchandler:registerNpc({
  id = 'np_prison:tower_guard',
  model = 's_m_m_prisguard_01',
  coords = vector4(1541.3, 2470.5, 62.9, 180.0),
  mode = 'networked',
  weapon = 'WEAPON_CARBINERIFLE',
  guard = { radius = 6.0 },
  routingBucket = 0,
})

Lifecycle ​

The server matches player positions against its grid every spawn.scopePollIntervalMs (2 s) and runs a pass every spawn.pollIntervalMs (1 s):

  1. A player counts for an NPC when they are in its routing bucket (bucket 0 when unset) and match its jobs / gangs. hours is ignored on the server: it has no in-game clock.
  2. With a counting player within spawnDistance (default 120 m), the server creates the ped: CreatePed(pedType, model, x, y, z, heading, true, true), SetEntityOrphanMode(entity, 2), SetEntityRoutingBucket, and the outfit, weapon, armour and freeze it can set itself. It checks that the entity exists with the right model, net id and bucket, and deletes it otherwise.
  3. The entity gets the state bag np_npchandler:npc with the NPC id, definition version and a generation number.
  4. The ped stays while a counting player is within spawnDistance + spawn.despawnMargin (15 m). Once nobody has been in range for despawnDelayMs (default spawn.despawnDelayMs, 15 s), it is deleted. A disabled NPC is deleted at once.
  5. At most spawn.maxSpawnsPerTick (4) peds are created per pass, nearest first, and at most spawn.maxNetworkedPeds (64) server-created peds exist at once, server immediate peds included.

The client that controls the ped applies the full definition: outfit, flags, relationship, combat values, voice, and the idle task or preset. After an ownership migration, the new owner re-applies the flags only and continues the behaviour (a patrol resumes at the nearest point).

Lifecycle states, as reported by getNpcState: defined, dormant, spawning, active, rehydrating, incapacitated, retiring, failed.

alwaysSpawned ​

With alwaysSpawned = true, the ped exists whether or not anyone is near. It is created on the next server pass after registration (within the spawn budget and cap) and never deleted for distance. This is the v0.2.0 anchor: persistence = 'anchor' means the same.

lua
exports.np_npchandler:registerNpc({
  id = 'np_shops:clerk_24_7_01',
  model = 'mp_m_shopkeep_01',
  coords = vector4(24.0, -1346.0, 29.5, 270.0),
  mode = 'networked',
  alwaysSpawned = true,
  scenario = 'stand_impatient',
})

Respawn ​

lua
respawn = { deleted = true, dead = true, delayMs = 5000, deadDelayMs = 15000, maxRetries = 3 }
Field Default Notes
deleted true A ped that was deleted, replaced (wrong model, net id or bucket) or can't be read is created again after delayMs. With false, it stays gone until every player has left its range; an alwaysSpawned NPC stays gone until it is updated or enabled again
dead true A dead ped stays as a body for deadDelayMs, then it is replaced after delayMs. With false, the body stays
delayMs spawn.rehydrateDelayMs (5000) 0 to 600000
deadDelayMs spawn.incapacitatedDelayMs (15000) 0 to 600000
maxRetries spawn.maxRetries (3) 0 to 100. Failed creations in a row before the server stops trying. The count resets once every player has left the NPC's range, and on update or enable

respawn is refused on local NPCs ('respawn_requires_networked').

Scoped state ​

Runtime state of a networked NPC only goes to players in its scope: in its bucket, matching its jobs / gangs, and within spawnDistance + spawn.scopeMargin (25 m).

  • np_npchandler:npcState carries the net id, generation, lifecycle and reaction behaviour. A player entering scope gets the current state at once.
  • Dialogue cues go to players in scope and to the player who triggered them.
  • A client drops the state of an NPC it has left, and the server stops sending it.

On the client ​

  • The client matches the ped to the NPC by net id and the server-written state bag (NPC id and generation), so a reused net id never gets the wrong ped tasked.
  • np_npchandler:spawned fires when the ped streams in for the client, np_npchandler:despawned when it streams out.
  • Every client renders the NPC's held props and interact option itself.
  • Outside a player's hours, jobs or gangs, the ped stays spawned for others. That player's client hides it locally every frame (SetEntityLocallyInvisible), lets the player pass through it, removes the interact option and does not task it.
  • Clients never delete server-created peds.

getNpcEntity(id) on the server returns entity, netId while the ped exists. On the client it returns the entity while it is streamed in.

Differences from local NPCs ​

Local Networked
Who creates it Each client The server
routingBucket unset All buckets Bucket 0
spawnDistance default 60 m 120 m
jobs / gangs Not spawned for players who don't match Spawned for players who match; hidden locally for the others
hours Hidden and deleted outside the hours Spawned regardless; hidden locally outside the hours
renderDistance Hides without deleting Not used
respawn, perception, allowRemoteControl Refused or not used Supported
frozen with patrol As set (default false) Always false (the controlling client walks the route)