Skip to main content

Exports

You can export functions from a resource so that other resources can call them.

Create exported function

First you have to make sure to define your function globally in your script:

function testExportFunc(arg1, arg2)
Console.Log("testExportFunc called: " .. arg1 .. ", " .. arg2)
return 400, 800
end

Define exported function

You will then need to define it in the meta file of your resource:

<meta type="lua">
<script type="server" src="server.lua" />
<export type="server" function="testExportFunc" />
</meta>

Call exported function

You can then call the function in several ways in any other resource:

local res1, res2 = Resource.Call("exporttest", "testExportFunc", { 8, 16 })
Console.Log("Result of testExportFunc: " .. res1 .. ", " .. res2)

res1, res2 = exports.exporttest:testExportFunc(8, 16)
Console.Log("Result of testExportFunc: " .. res1 .. ", " .. res2)

res1, res2 = exports["exporttest"]:testExportFunc(8, 16)
Console.Log("Result of testExportFunc: " .. res1 .. ", " .. res2)
info

A resource inside a category folder is addressed by its full name, which contains a / and therefore only works with the bracket form:

exports["gamemodes/freeroam"]:testExportFunc(8, 16)
warning

exports only reaches resources that are currently started. If the target resource is not running, the call quietly returns nil/null and writes a line to the log, so a typo in the resource name looks the same as a function that returned nothing. Resource.Call raises an error instead, which is easier to debug.