Jump to content

Reference to resource's exported functions


Recommended Posts

If I have a resource called 'theResource' and this resource has a few exported functions that I am using in another resource, you just call the exported function like this:

local info = exports.theResource:getInfo("parameter")

This calls the export as expected. To make this more convenient for cases where you need to call the resource many times such as using multiple exported functions, the reference to the resource's exported functions can be shortened down to:

resource = exports.theResource -- Save the reference to all exported functions from the resource called 'theResource'

local info = resource:getInfo("parameter")

The problem however is if the resource called 'theResource' is ever restarted, all calls to any exports using the variable 'resource' no longer work since of course the existing reference to the resource root has changed as it was restarted.

Now all the export calls do not work and throw errors. Is there a way to obtain the reference to the resource again, preferably without the use of event handlers?

Link to comment
5 minutes ago, N3xT said:

As far as I know, the only way to solve this is to use event handlers

so I'd recommend you to use onResourceStart or onClientResourceStart and re-define the reference.

A shame, the way I am currently doing it is with onClientResourceStart. I was hoping there might be a more efficient way to do it - guess I'll just have to continue adding an event handler in each file that makes use of this shorthand reference to a resource's exports.

Edited by GenericDeveloper
  • Like 1
Link to comment
  • Moderators
5 hours ago, GenericDeveloper said:

guess I'll just have to continue adding an event handler in each file that makes use of this shorthand reference to a resource's exports.

You can also use call instead of exports.

Which uses a static resource pointer that doesn't change when a resource restarts.

Less fancy, I know. But you can use it to make a wrapper when you overwrite exports with a meta table.

Edited by IIYAMA
Link to comment
12 minutes ago, IIYAMA said:

You can also use call instead of exports.

Which uses a static resource pointer that doesn't change when a resource restarts.

Less fancy, I know. But you can use it to make a wrapper when you overwrite exports with a meta table.

But how would you use call to get all of the exported functions? As far as I know, you can only use call for a specific export function call in which case I may aswell just write exports.myResource:functionName every time.

Link to comment
  • Moderators
25 minutes ago, GenericDeveloper said:

But how would you use call to get all of the exported functions? As far as I know, you can only use call for a specific export function call in which case I may aswell just write exports.myResource:functionName every time.

After you have set-up your meta table + overwrite exports.

https://www.tutorialspoint.com/Lua/lua_metatables.htm

--[[ ... ]]
__index = function(mytable, key)
  
  local resource = getResourceFromName(key)
  
  if not resource then
    return false
  end
  
  return function (...)
    return call(resource, ...)
  end
end
--[[ ... ]]

 

It is not 100% the same, but you got the idea.

Edited by IIYAMA
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...