I want to export a client function from my resource to be able to use it from other resources.
When the player joins the game, the resource cannot find the exported functions:
[ 47797] Error resuming coroutine: citizen:/scripting/lua/scheduler.lua:699: No such export MyExportedFunction in resource bob74_export
[ 47797] stack traceback:
[ 47797] [C]: in function ‘error’
[ 47812] citizen:/scripting/lua/scheduler.lua:699: in metamethod ‘__index’
[ 47812] client.lua:2: in function <client.lua:1>
If I restart the import resource (bob74_import) from the server console, it finds the function and return the value in the console as expected.
If I disconnect and reconnect the game, I come back to the same issue (No such export MyExportedFunction in resource...).
Since I was unable to get it to work properly, I stripped it to this test environment:
Latest server version and server data
Brand new server.cfg
Server cache deleted
Only two resources added (and loaded in this order): bob74_export bob74_import
The point is to get a table from another resource and show one of its values using Citizen.trace()
Thanks! I added a dependency into the import resource (dependency 'bob74_export') but it still didn’t work.
Waiting before retrieving the exported function works well but it seems to be quite a messy way to do.
Aren’t there any reliant way to do this? I was really hopping adding a dependency would do the trick, otherwise I don’t see its purpose.
Alright, I guess they will change this behaviour later ^^
Thanks for your answers, using a timeout looks like a nice solution to keep things clean
EDIT:
This should work fine:
local ImportedValue = nil
Citizen.CreateThread(function()
local errorMsg = ""
-- Try to read each 250ms and timeout after 5 seconds
for i = 0, 20, 1 do
local status
-- Calling the function in protected mode
status, errorMsg = pcall(function() ImportedValue = exports['bob74_export']:MyExportedFunction() end)
if status then
break
end
Citizen.Wait(250)
end
if (ImportedValue == nil) then
Citizen.Trace("ERROR: " .. errorMsg)
return
end
Citizen.Trace(ImportedValue.value)
end)
I find it easier to see what functions are available from the resource when it is listed in the manifest. Is there a difference between the exports function and using export in the manifest?
Also, I noticed this part:
Note that these exports will only be available after the first scheduler tick
It could explain why I’m unable to call the method as soon as the resource is loaded.
Using exports(), I can call my functions immediately whereas using export 'MyFunction', it is only available after the next tick (using Wait(0))
That’s the “after the first scheduler tick” part of the documentation I guess. I didn’t get it last time.
Thanks a lot to all of you, it is a lot clearer now