James
James

Reputation: 83

Lua - Getting a Localized Variable or Function without using the Debug Library with 2 seperate scripts

Ok so I am using a object orientated environment, I can make several scripts that may or may not connect with each other, all depending on if I want them to connect or not, and I want to get a function that is localized local myfunc = function() end from a different object that can handle code, from this point forward I will be calling these objects "Scripts" as they are what they are called in the game and are easily used for me to tell people what I am talking about even if they are not formally used as a name to suggest such a thing.

So lets say I have Script 1 with this code:

local myfunc = function() return true end

and I have Script 2 with a blank sheet, I want to make it so I can get myfunc without touching the debug library, making the original script a module script and returning the function, and this has to stay in 2 separate scripts. That is all for the requirements if you are wondering. I expect this can be done and I hope someone out there has the knowledge on how to do something like this clean and efficiently!

Upvotes: 0

Views: 271

Answers (2)

luther
luther

Reputation: 5554

Lua chunks can have return statements. To return a single function:

return function()
  return true
end

To return a table with multiple functions:

return {
  myFunc = function()
    return true
  end,
  myOtherFunc = function()
    return false
  end,
}

Upvotes: 0

Nicol Bolas
Nicol Bolas

Reputation: 473447

The whole point of a local variable is that it's local; other people can't touch it. The traditional means of having one script access data from another are modules or global variables. Both options which you have declared that you can't/won't do.

Your requirements reduce the set of possible solutions to zero.

Upvotes: 1

Related Questions