tm.exports
Cross-resource handles and soft-dependency helpers. Shared. Use these instead of exports.res:Fn(...): a handle waits (bounded) for a resource that is still starting and for an export its scripts have not registered yet, so nothing depends on ensure order. Stopped and missing resources fail fast, so a pcall'd call to an optional resource that is switched off never stalls.
local inv = tm.exports.get('some_resource')
local ok, err = pcall(function() return inv:DoThing(1, 2) end) -- colon call (self is ignored)
local dot = tm.exports.getResource('some_resource')
dot.DoThing(1, 2) -- dot call
tm.exports.onStart('some_resource', function() -- soft dependency: runs now if started, and again after every restart
tm.exports.get('some_resource'):RegisterThing('mine')
end)
if tm.exports.isStarted('some_resource') then ... end -- cached, cheap in a loopA resource you cannot work without belongs in dependencies instead. Call the exact exported name, and do not probe with type(x.fn) == 'function' (the handle creates a function for any key).
Waits (configurable)
Two bounded waits apply, and only when the calling thread can yield (a thread, or a callback that runs on one). A caller that cannot yield (a plain event handler) errors immediately instead of waiting.
| Option | Default | Meaning |
|---|---|---|
startWait | 5000 ms | How long a call waits for a resource whose state is starting or uninitialized. |
registerWait | 15000 ms | How long a call waits for a started resource to register an export its scripts have not reached yet (a resource counts as started before its scripts finish loading). It stops early when the resource leaves the started state. |
A value of 0 means never wait (fail at once). Set them per handle, per call, or as the default:
tm.exports.get('res', { registerWait = 0 }):Fn() -- this handle never waits for a late export
tm.exports.getResource('res', { startWait = 1000 }).Fn()
tm.exports.call('res', 'Fn', { startWait = 1000, registerWait = 2000 }, arg1, arg2) -- one call
tm.exports.configure({ registerWait = 5000 }) -- new default for this resourceA handle made with options is not cached. A handle made without options is cached per resource name and shares the defaults.
Functions
| Function | Notes |
|---|---|
get(name, wait?) | Handle for colon calls. |
getResource(name, wait?) | Handle for dot calls. |
getRaw(name, wait?) | The resource's raw exports table once it is started. |
call(name, key, wait?, ...) | One call with its own waits. Pass nil for wait to use the defaults. |
configure(wait) | Changes the defaults for this resource. Omitted or invalid values keep their old value. |
onStart(name, fn) | Runs fn now when name is started, and again every time name starts. The hook after a restart runs one tick after the start event so the resource's own start handlers have set up first. A failing hook is logged, never thrown. |
isStarted(name) | Whether name is started. Cached and kept current by start and stop events. |
Errors read [exports] resource 'x' not started (state=stopped) or [exports] 'x:Fn' not found.