I recently picked up coding in Lua/FiveM after a few years, and I was wondering how to make an function I made in my own resource public for others to use in their resource? For example ESX functions, oxmysql, etc..
I saw this post from 2020, is this still the best way?
You can use events if you don’t need a return value but (at least for me) it kills the flow of the code.
I personally do it similar to this:
local function DoTheThing(some, params, here)
-- do whatever
end
exports("DoTheThing", function(...)
print("Resource " .. GetInvokingResource() .. " executed DoTheThing export")
return DoTheThing(...)
end)
This allows for logging the resource that called the export if there should be issues in the calling code.
However there are some things to keep in mind:
Exports are not particularly performance friendly. Do not create one that will be called in e.g. a Wait(0) loop. Only call when necessary and build the functions behind the exports with this in mind.
Exports are blocking. If you call a Wait without an additional CreateThread around it, it will cause the calling code to wait til export execution is finished.
Edit: Do not define exports in the fxmanifest. This is not recommended as these will only be available after the first scheduler tick.
Thanks so much for the fast and good reply! I’ll probably look into exports since they will be database triggers (which are not called in a Wait(0) loop)