everyone says “oh yeah perfect” because they know that they have to try to keep everything “local” - and the module function creates global objects.
so basically, you index all your functions inside a table and “return” that table to any other module that asks for it (via “require”).
in that way your functions won’t fly around in space (memory) and won’t block variable names.
try this (bad code):
main.lua
require ( "mod1" ) -- require first module without function call
require ( "mod2" ).myFunction () -- call the function of second module
mod1.lua
module(..., package.seeall)
function myFunction ()
print ( "MOD 1" )
end
mod2.lua
module(..., package.seeall)
function myFunction ()
print ( "MOD 2" )
mod1.myFunction ()
end
when you start this, the output will be
MOD 2
MOD 1
mod2 is able to call the function of mod1 because mod1 is global.
you don’t really want that. It’s LUA-rule #1: keep things local.
change the mod.lua files
to this (good code):
mod1.lua
local M = {}
function M.myFunction ()
print ( "MOD 1" )
end
return M
mod2.lua
local M = {}
function M.myFunction ()
print ( "MOD 2" )
mod1.myFunction () -- ERROR!!!
end
return M
This will invoke an error, because mod1 isn’t global anymore.
If you want to call a function of mod1, you have to require it in.
mod2.lua
local M = {}
function M.myFunction ()
print ( "MOD 2" )
require ( "mod1" ).myFunction () -- output: MOD 1
end
return M
If you want to get some data from a module, return something:
mod1.lua
local M = {}
function M.makeRandomNumber ( range )
local newNumber1 = math.random ( range )
print ( "MOD 1 newNumber1: "..newNumber1 )
return newNumber1
end
return M
mod2.lua
local M = {}
function M.myFunction ()
local newNumber2 = require ( "mod1" ).makeRandomNumber ( 10 )
print ( "MOD 2 newNumber2: "..newNumber2 )
end
return M
output would be:
MOD 1 newNumber1: 4
MOD 2 newNumber2: 4
or any other number between 1 - 10…
I hope that clears anything up 
-finefin
[import]uid: 70635 topic_id: 15066 reply_id: 55921[/import]