Decision
main.lua
Foo = Foo or require "foo" Bar = Bar or require "bar" print (Foo.getName()) print (Bar.getName())
foo.lua
Foo = {} Bar = Bar or require "bar" Foo.name = 'foo' function Foo:getName() return Foo.name .. Bar.name end return Foo
bar.lua
Bar = {} Foo = Foo or require "foo" Bar.name = 'bar' function Bar:getName() return Bar.name .. Foo.name end return Bar
Explanation
Since you are setting global variables, you can check if a file (aka; global already defined) is required before trying another:
Bar = Bar or require "bar"
Then your bar.lua will have to return the definition of Bar;
Bar = {} -- ... return Bar
This will not completely solve the problem, since bar.lua expects Foo to determine. To solve this problem, you can define a dummy variable with the same name:
Foo = {} Bar = Bar or require "bar"
This is only possible because you delay the use of Foo until the function is called. If you want to call Foo.name from the bar.lua , you will have the same circular dependency problem.
Jess telford
source share