I struggled with this concept as well. The pattern I ended up using is as follows:
The main.lua file:
-- main.lua
local bob = require("Bob")
local sally = require("Sally")
bob:init( {sally = sally} )
sally:init( {bob = bob} )
The Bob.lua file:
-- Bob.lua
module(..., package.seeall)
local bob = {}
-- Forward reference sally here:
local sally = {}
bob.foo = "bobs foo"
bob.bar = "bobs bar"
function bob:init(params)
sally = params.sally
end
-- This "return" at the end of the file, returns everything in the "bob" table to the variable used during the "require" statement.
return bob
The Sally.lua file:
-- Sally.lua
module(..., package.seeall)
local sally = {}
-- Forward reference bob here:
local bob = {}
sally.foo = "sallys foo"
sally.bar = "sallys bar"
function sally:init(params)
bob = params.bob
end
-- This "return" at the end of the file, returns everything in the "sally" table to the variable used during the "require" statement.
return sally
Now everyone can interact with everyone.
Example:
In the Bob.lua file, we can get sally’s foo with the “getSallysFoo” function as:
-- Bob.lua
module(..., package.seeall)
local bob = {}
-- Forward reference sally here:
local sally = {}
bob.foo = "bobs foo"
bob.bar = "bobs bar"
function bob:printSallysFoo()
-- Since sally was forward referenced, we don't get compile errors
print ("sally.foo:" .. sally.foo)
end
function bob:init(params)
-- Now we are populating the dummy "forward referenced" variable with a real reference. Now we can get / set anything that is in the Sally.lua file, so long as the stuff in Sally.lua that we want to fiddle with is preceded with "sally." (e.g. sally.whatever" or "sally:whatever" in the Sally.lua file)
sally = params.sally
-- Call the fundtion once initialized:
bob:printSallysFoo()
end
return bob
As you can see, we can now call that function to sallys foo from the main:
-- main.lua
local bob = require("Bob")
local sally = require("Sally")
bob:init( {sally = sally} )
sally:init( {bob = bob} )
-- Since everything is all initialized here, we can now print sallys foo here too:
bob:printSallysFoo()
Hope this all makes sense (I didn’t run the code, so there may be typing errors 
But it’s the pattern I’m trying to render.
[import]uid: 616 topic_id: 20272 reply_id: 81236[/import]