I have been separating my game code into several files, one for each part of it (mainMenu.lua,HUD.lua,arcadeMode.lua) and importing them with require "file" but doing things this way , as the code grows things start to get confusing because of the huge amount of functions i have created.
local base = _G
module("api")
function foo()
math.random(1, 100)
-- error, "math" is undefined
end
function bar()
base.math.random(1, 100)
-- the proper way to access "math"
end
There, both foo and bar are now accessed via "api.foo()" or "api.bar()".
However, using the module keyword makes globals inaccessible so you it's good to have a "base" reference.
T-Bone wrote:The problem there is that it assumes that the module you are importing is well written.
That is unfortunate, yes. However, if you write the module yourself, you can make it well written. If it's a 3rd party library, you should probably use one that's well written anyway.