Installation ​
Requirements ​
- A recent FiveM server artifact (Lua 5.4, state bags).
- OneSync for networked NPCs and networked immediate peds. With
spawn.requireOneSync = true(the default), the server refuses to create peds whileonesyncis off and reports'onesync_required'. Local NPCs and preview peds work without it.
Nothing else is required. There is no database and no NUI, so there is nothing to build.
Optional integrations ​
Everything below is detected at runtime with GetResourceState, and every call into it is protected.
| Kind | Resources | What it adds |
|---|---|---|
| Framework | es_extended, then qbx_core, then qb-core |
Job and gang for the jobs / gangs conditions. On ESX the gang is xPlayer.gang, or job2 when the framework has it. Without a framework, NPCs with jobs or gangs are never available |
| Interaction | np_hud, then ox_target, then qb-target |
Shows the interact option. With none of them, options are not shown unless interaction.fallback = true |
| Held props | np_prophandler |
NPC held props go through its attach export. Without it, np_npchandler attaches a local object itself |
| Staff | np_admin |
The np_npchandler.debug permission node for npchandler_debug, and live settings. See Configuration → Editable in np_admin |
The framework is read again when its job events fire and every 10 seconds on the client, so the order in
server.cfg does not matter for detection.
Steps ​
- Put the resource in
resources/asnp_npchandler. Other resources call its exports by that name (exports.np_npchandler:registerNpc(…)). - Add it to
server.cfgbefore the resources that use it:cfgset onesync on ensure es_extended # or qbx_core / qb-core, optional ensure np_hud # or ox_target / qb-target, optional ensure np_admin # optional ensure np_prophandler # optional, held props ensure np_npchandler # np_shops, np_gangs, np_prison … and every other resource that uses it, after it - Open
shared/config.lua. The defaults are a working setup. See Configuration.
Registering: np_npchandler:ready ​
The registry lives in memory. When np_npchandler restarts, it is empty, and every resource that uses it must
register its NPCs, population overrides, predicates and adapters again. np_npchandler:ready fires on the server once
it has started, and on the client once its first definition snapshot arrived. Both fire again after a restart.
Register in a handler and once on your own start, instead of watching onResourceStart or onClientResourceStart:
-- server
local function registerAll()
exports.np_npchandler:registerNpcs(myNpcs)
end
AddEventHandler('np_npchandler:ready', registerAll) -- { enabled, epoch }
if GetResourceState('np_npchandler') == 'started' then registerAll() end
-- client: local NPCs and setCanInteract predicates go with np_npchandler too
local function registerLocal()
exports.np_npchandler:addLocalNpc({ ... }) -- 'id_in_use' when both ran is harmless
end
AddEventHandler('np_npchandler:ready', registerLocal)
if GetResourceState('np_npchandler') == 'started' then registerLocal() end
It is the same pattern as np_prophandler:ready. See
Events.
Exports from a resource that isn't running throw. The np_* resources that use np_npchandler list it in their
dependencies (Used by), so they do not start without it. If np_npchandler is optional
for your own resource, check GetResourceState('np_npchandler') == 'started' or wrap the call in pcall, and keep
your own fallback.
Check it works ​
- Run
npchandler_debugin the server console. It prints the definitions by mode, networked peds spawned, dormant and failed, immediate peds and population overrides:[np_npchandler] definitions=12 local=10 networked=2 spawned=1 dormant=1 failed=0 immediate=0/0 population=0 - As a player allowed
np_npchandler.debug, run/npchandler_debug. F8 prints the client counts every 2 seconds:Run it again to stop the print.[np_npchandler] registered=12 available=12 spawned=3 hidden=0 loading=0 failed=0 networked=1 immediate=0/0 population=0 provider=np_hud - Walk away from a local NPC: it disappears at
spawnDistance + 10 mand is deleted 10 seconds later.
Turning parts off ​
| Want | Set |
|---|---|
| The whole resource off | enabled = false. No ped is spawned and client requests fail closed. Registrations are still accepted |
| No server-created peds | spawn.enabled = false |
| No autonomous reactions | client.perceptionEnabled = false |
| No interaction prompts | interaction.provider = 'none' |
| Only certain models, scenarios, anim dicts, weapons, roles | restrict.models = { mp_m_shopkeep_01 = true, … } and the other restrict lists |