Addons ​
An addon is its own resource that extends one host resource through the host's public API:
- np_cellnet is an addon of np_phone: towers, carriers and signal.
- np_spy-pigeon is an addon of np_surveillance: surveillance pigeons.
- np_medic_field is an addon of np_medic: field hospital camps.
The addon never edits the host, and the host never depends on the addon. Every np_* host and addon follows the same
five rules below. The lifecycle part is implemented by one small lib, lib/np_addon.lua, which lives in
np_admin next to lib/np_admin.lua and is vendored byte-identical, like that one.
This page is for resource authors. Server owners only need to know that an addon is started like any other resource, in any order: it attaches to its host on its own, also after a restart of either.
1. Vendor the lib on both sides ​
Copy np_admin/lib/np_addon.lua to bridge/np_addon.lua, in the host and in every addon, and load it as a shared
script:
shared_scripts {
'shared/config.lua',
'bridge/np_admin.lua', -- if you use np_admin
'bridge/np_addon.lua',
}
It depends on nothing, not even np_admin. Don't use @np_admin/... includes: they would make np_admin mandatory.
2. Host: declare an API level, say when you are ready ​
-- shared (both sides), early
npAddon.host({ api = 1, label = 'Phone' })
-- server, at the very end of your boot
-- (schema done, registries empty and accepting)
npAddon.ready()
ready() publishes GlobalState['npAddon:<host>'] = { api, version, boot }, replicated to every client. A new boot
id tells every addon, on the server and on each client, to attach again. That happens after every host restart, in any
start order. The host clears the key itself when it stops.
Bump api whenever you break an extension point. An addon that needs a newer API is refused with a clear console
line instead of failing halfway.
On the server the host can also list its addons: npAddon.addons() (attached now), npAddon.installed() (every
resource whose manifest names this host, started or not) and npAddon.onAttach(fn) (addons coming and going).
3. Host: every extension point is owner-tracked ​
Each export that lets another resource register something must remove that registration when the caller stops:
exports('registerThing', function(name, def)
local owner = npAddon.owner() -- GetInvokingResource(); nil for the host itself
things[name] = def
npAddon.onOwnerStop(owner, function() things[name] = nil end)
return true
end)
Export naming ​
| Kind | Name | Returns |
|---|---|---|
| Registry | register<Thing>(name, def) / unregister<Thing>(name) |
true | false, 'code' |
| Veto or observe hook | addHook(name, fn) / removeHook(id); fn returns false, 'code' to cancel |
id |
| Single slot | register<Role>Provider(name, impl), keyed by name. Never a last-writer-wins set<X>Provider(fn) |
true | false, 'code' |
| State an addon owns | set<Thing>(target, state), cleared when the owner stops |
true | false, 'code' |
All exports are camelCase. Error codes are short snake_case strings (not_ready, invalid_name) that the host's UI can
translate. np_phone's setNetwork is an example of the last row: the state belongs to the provider and disappears with
it (np_phone → Network).
4. Addon: declare it, attach through the lib ​
-- fxmanifest.lua
np_addon 'np_phone' -- machine-readable "addon of"; np_admin and the host can list it
dependency 'np_phone' -- only if the addon is useless without the host
-- server and/or client
npAddon.attach('np_phone', function(host)
-- host = { name, api, version, boot }; runs now if the host is ready, and again after every host restart
exports.np_phone:addHook('rpc', onRpc)
exports.np_phone:registerStoreApp(App)
end, { api = 1, detach = function() Signal.pause() end })
Rules for the attach function:
- Register everything you hand to the host inside it. No hand-written
onResourceStartor'<host>:ready'handlers, and noWait(1500)races. - Keep it idempotent. It runs once per host boot, and the host has already forgotten your previous registrations.
- Don't undo registrations in
detach: the host's owner tracking does that. Usedetachto pause your own loops while the host is away.
npAddon.attach returns a handle with active() and host(); npAddon.isAttached('np_phone') answers the same from
anywhere.
5. Optional siblings are not hosts ​
An addon extends one host. Everything else it merely uses when present, such as np_hud toasts, np_inventory items or
np_admin settings, goes through the integrations registry (integrations/00_registry.lua, one adapter per sibling,
each with a fallback, as in np_fishing or np_cellnet). The two fit
together:
npAddon.attach |
Integration.define |
|
|---|---|---|
| For | The host you extend | Siblings you can live without |
| Required | Yes (np_addon, usually dependency) |
No, each has a fallback |
| Cleanup | The host's owner tracking | Your adapter re-registers when the sibling restarts |
npAddon.attach works for any host, not only the one named in np_addon. An adapter whose sibling is also a host can
use it to re-register after the sibling restarts.
Checklist ​
Host
- [ ]
bridge/np_addon.luavendored byte-identical - [ ]
npAddon.host({ api })set, andnpAddon.ready()called at the end of the server boot - [ ] every
register*/add*/set*export is owner-tracked throughnpAddon.owner()/onOwnerStop - [ ] the docs list the extension points and the API level
Addon
- [ ]
np_addon '<host>'in the manifest (plusdependencyif it is required) - [ ]
bridge/np_addon.luavendored byte-identical - [ ] all host registrations live inside
npAddon.attach(host, fn, { api }) - [ ] optional siblings go through
integrations/, each with a fallback - [ ] the docs say "addon of <host>" and name the minimum host API
"Addon" is one word everywhere: code, docs and the landing page.
Hosts today ​
| Host | API | Extension points | Addons |
|---|---|---|---|
| np_phone | 1 | Hooks, RPCs, App Store apps, blocks, bridge overrides, network states, RPC needs (Exports → Addons) | np_cellnet |
| np_medic | 1 | The feature check can(src, feature), the client event np_medic:notify, treatment prop lists that may name addon models (Exports → Addon host) |
np_medic_field |