Jump to content

Creating variable reference to exported function


Recommended Posts

Posted

I am trying to create an array which references exported variables from another resource however it seems that the call to the function is actually invoked rather than being saved as a reference to the variable.

-- Array of functions we want to have a reference to, includes exported functions from other resources.
local myFunctions = {
    ["FUNCTION_1"] = exports.myresource:myFunction,
    ["FUNCTION_2"] = exports.myresource:anotherFunction,
    ["FUNCTION_3"] = getElementPosition,
    ["FUNCTION_4"] = exports.myresource:finalFunction,
}

function executeFunctions(functionList, parameter)
    for i, func in ipairs(functionList) do -- For every function we need to call.
        functionToInvoke = myFunctions[func] -- Get the function reference from our array.
        functionToInvoke(parameter) -- Invoke the function and pass the parameter into it.
    end
end

executeFunctions({"FUNCTION_1", "FUNCTION_4"}, "some parameter value") -- Call executeFunctions() to recursively execute function 1 and 4 with our parameter.

While myFunctions["FUNCTION_3"] works and references the native getElementPosition function, the references to exported functions don't work.

Is this possible at all?

Posted (edited)
6 hours ago, GenericDeveloper said:

I am trying to create an array which references exported variables from another resource

Correction, this should say exported functions from another resource, not variables!

Edited by GenericDeveloper
  • Moderators
Posted
3 hours ago, GenericDeveloper said:

Correction, this should say exported functions from another resource, not variables!

Try this

local myFunctions = {


["FUNCTION_1"] = function (...)
return exports.myresource:myFunction(...)
end


}

 

Predefining export functions from another resource is not a good idea. Because when restarting that resource, all export functions will be reset.

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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...