Exports
Let resources call each other's functions.
Developer Preview. Scripting is live on Neighborly servers – everything on these pages works today. The API is young, so tell us what you need next on Discord.
Exports let resources call each other's functions. A bank resource can export getBalance, and your café, phone and housing resources can all use it – without copying any code.
Exporting a function
Call exports(name, fn) in a client or server script. Exports belong to the side they were created on: a server export can only be called from server scripts, a client export only from client scripts.
local db = exports.neighborly:db()
exports('getSavings', function(player)
return db:get('savings:' .. GetPlayerAccountId(player)) or 0
end)
exports('addSavings', function(player, amount, reason)
if type(amount) ~= 'number' or amount <= 0 then
return false
end
local caller = GetInvokingResource() or GetCurrentResourceName()
db:increment('savings:' .. GetPlayerAccountId(player), amount)
print(('%s added %d to savings (%s)'):format(caller, amount, reason or 'no reason'))
return true
end)Calling an export
Use exports.resource_name:exportName(...). The colon matters. If the resource name contains a dash, use brackets: exports['rtx-bank']:getSavings(src).
RegisterNetEvent('rtx_cafe:payout', function()
local src = source
local tips = Player(src).state.tipsToday or 0
if tips > 0 then
exports.rtx_bank:addSavings(src, tips, 'Café tips')
Player(src).state:set('tipsToday', 0, true)
end
end)Calling an export of a resource that isn't running raises an error. Add the resource to dependencies in your manifest so it is always started first, or check and protect the call when the integration is optional:
local function addToSavings(player, amount)
if GetResourceState('rtx_bank') ~= 'started' then
return GiveMoney(player, amount, 'Café tips')
end
local ok, result = pcall(function()
return exports.rtx_bank:addSavings(player, amount, 'Café tips')
end)
return ok and result
endHelpers
| Function | Returns | Description |
|---|---|---|
GetCurrentResourceName() | string | The name of the resource the code runs in. Use it instead of hard-coding your name. |
GetInvokingResource() | string? | Inside an export: the resource that called it. nil when called from your own resource. |
GetResourceState(name) | string | 'started', 'starting', 'stopped', 'stopping', 'uninitialized' or 'missing'. |
When the other resource restarts
Things you registered in another resource through its exports – a phone app, an inventory item, a menu entry – disappear when that resource restarts, because its memory starts fresh. Register them again when it starts:
local function addCafeApp()
exports.rtx_phone:addApp({ id = 'cafe', label = 'Bluejay Café', event = 'rtx_cafe:openApp' })
end
AddEventHandler('onClientResourceStart', function(name)
if name == 'rtx_phone' then
addCafeApp()
end
end)
if GetResourceState('rtx_phone') == 'started' then
addCafeApp()
end
Treat exports like a public API. Once other people build on them, renaming or changing parameters breaks their resources – add new exports instead, and bump your major version for breaking changes.