Skip to main content

Exports

You can export functions from a resource so that other resources can call them. The calling resource does not have to be written in the same language as the exporting one.

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)
Return values

A function can return several values only in Lua. When a Lua function returns more than one value, a JavaScript caller receives them as an array (const [a, b] = Exports.res.fn()) and a Squirrel caller receives only the last one. A JavaScript or Squirrel function always returns exactly one value; to hand back several, return an array or object, which a Lua caller receives as a table.

warning

Exports only reaches resources that are currently started. If the target resource is not running, the call quietly returns nil/null/undefined 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.

note

Lua and Squirrel also accept the old lowercase name exports. It behaves exactly like Exports and logs a deprecation notice the first time a resource uses it. JavaScript only knows Exports.