Skip to content

np_npchandler v1.1.0 ​

One place where resources get their NPCs from. A resource describes an NPC once, and np_npchandler does the rest: validating it, deciding whether a player should see it, spawning it near players and removing it when they leave, hiding it before deleting it, loading and releasing its model, dressing and arming it, running its idle behaviour, offering its interaction, reacting to players, and cleaning up when the owning resource stops. One streaming loop and one set of budgets replace the ped loops each resource used to run on its own.

It is standalone. A framework is only used for job and gang conditions. np_hud, ox_target or qb-target show the interaction prompts, np_prophandler renders held props, and np_admin gates the debug command and edits the settings. None of them is required. There is no database and no NUI.

Its counterpart for objects is np_prophandler.

Used by ​

These np_* resources list np_npchandler in their dependencies and spawn their NPCs through it: np_banking, np_character, np_clothing, np_darts, np_drugs, np_environmental, np_farming, np_fishing, np_gangs, np_golf, np_identification, np_metal-scanner, np_prison, np_shops, np_spy-pigeon. Start np_npchandler before them. What each one registers is listed in Migrating from CreatePed.

Register once, the handler does the rest ​

lua
-- server, in np_gangs
exports.np_npchandler:registerNpc({
  id = 'np_gangs:lester',
  model = 'ig_lestercrest',
  coords = vector4(1273.9, -1719.7, 54.77, 25.0),
  scenario = 'WORLD_HUMAN_CLIPBOARD',
  spawnDistance = 60.0,
  interact = { label = 'Talk', icon = 'comments', event = 'np_gangs:lester:open' },
})

-- client, in np_gangs
AddEventHandler('np_gangs:lester:open', function(id, entity, data)
  -- open the menu
end)

From then on:

Step Who does it
Availability np_npchandler. An NPC is available to a player when it is enabled, its routing bucket matches and its job, gang and hour conditions match
Spawn / despawn np_npchandler. Local NPCs are created on each client inside spawnDistance, nearest first. Networked NPCs are created by the server while a player is near
Render / derender np_npchandler. Out of range, a local NPC is hidden at once and deleted later. An optional renderDistance hides it without deleting it
Look and behaviour np_npchandler. Outfit, weapon, flags, relationship, combat values, scenario or anim, and the wander / patrol / guard presets
Clean-up np_npchandler. Everything a resource registered or created is removed when that resource stops
Reacting Your resource. Listen to the interact event or np_npchandler:spawned. Do not delete the ped yourself; remove or disable the NPC instead

Kinds of NPCs ​

Kind Where it lives Use it for Page
Local NPCs (mode = 'local', default) A client-local ped on each client near it Shopkeepers, desk clerks, Lester, dealers, decoration Local NPCs
Networked NPCs (mode = 'networked') One OneSync ped, created by the server while players are near (or always, with alwaysSpawned) Guards and clerks everyone must see in the same place, NPCs with reactions Networked NPCs
Immediate peds Created now for the caller, not streamed, optionally as a clone of the player Preview peds, corner customers, k9 dogs, mission peds, a player's body Immediate peds

Around them:

How it works ​

  • Registry on the server. NPCs registered through server exports belong to the resource that called the export (GetInvokingResource()). Only that resource can change or remove them.
  • Sync. A client gets one snapshot when it starts, then small state messages with consecutive revisions. The same stream carries server population overrides. See Events → Sync.
  • Streaming on both sides. Local NPCs sit in a 2D grid on each client, networked NPCs in a grid on the server. Passes look only at nearby cells and are skipped while the player stands still.
  • Ready event. np_npchandler:ready fires on the server and on the client, also after a restart of the handler. Register in a handler for it and once on your own start. See Installation.
  • Conditions are not security. jobs, gangs, hours and client predicates decide what a player is shown. Anything an NPC triggers must be validated by your own server code.

Status ​

Not yet run on a live server

Version 1.1.0 was written against stubbed natives. The plain Lua specs in tests/ load the real scripts and pass: validation, local and networked streaming, the render tier, conditions, sync, budgets, model failures, ownership, immediate peds and creation tickets, held props, interaction providers and option lists, client predicates, clones, the client ready event, runtime control, patrol, population, the v0.2.0 API and reactions, and the debug gate. Spawning, ownership migration, tasks, speech, interaction providers, population natives and state bag replication have not been checked in game.

What needs a live test on a OneSync server with two clients before you rely on it:

  • The server-side natives used when a networked ped is created (SetPedComponentVariation, SetPedPropIndex, GiveWeaponToPed, SetPedArmour, CreatePedInsideVehicle, …). They are called protected, and the controlling client applies the same values again, so a missing server native only delays the look.
  • That an entity state bag set by the server reaches clients that receive the entity later.
  • The server's state bag guard against client writes. With sv_stateBagStrictMode on, client writes are refused anyway.
  • SetEntityLocallyInvisible every frame for networked NPCs outside a player's hours, job or gang.
  • Native argument lists for patrol and walkTo (TaskFollowNavMeshToCoord), TaskWanderInArea, TaskGuardCurrentPosition, the scenario blocking and vehicle generator natives, and AddRelationshipGroup.
  • That ClearAreaOfPeds leaves the handler's own peds alone.
  • Ambient speech on a networked ped the client does not own.
  • The np_hud, ox_target and qb-target option shapes, with lists: ox_target removeLocalEntity(entity, { names }), qb-target RemoveTargetEntity(entity, { labels }) and qb-target passing the distance to canInteract.
  • The arrow keys (controls 172 / 173) for shared keys not clashing with a phone or menu resource.
  • clone: ClonePed on a player ped, cloning a hidden local copy into a networked ped, and the Lua returns of GetPedHeadOverlayData and GetPedDecorations.
  • Ownership migration: the new owner re-applies flags and the idle task. Whether that shows as a twitch.

Not supported:

  • There is no invisible option. np_clothing hides its catalogue scan peds itself.
  • Runtime control (playAnim, walkTo, stopTask, …) is client-side only and acts on peds that client controls. There is no server export to task a networked ped.
  • Vehicles are not handled. createNetworkedPedInVehicle and spawnPedInVehicle need a vehicle the resource created itself (np_gangs' patrol car, np_prison's intake bus).
  • Without np_hud, ox_target or qb-target, options are not shown unless interaction.fallback = true.

Version 1.1.0 ​

Every 1.0.0 call works unchanged. New:

  • interact lists: up to six options per NPC, each with its own name, key, event, minigame and jobs / gangs / hours. The option name is the 5th event argument and redeemInteraction takes it. See Interaction.
  • Client predicates: canInteract on client definitions and setCanInteract.
  • clone: 'player' or a ped handle on createLocalPed, createNetworkedPed and createNetworkedPedInVehicle copies that ped's look.
  • np_npchandler:ready on the client too, after the first snapshot and again after a restart.

Next steps ​