idan ahal
idan ahal

Reputation: 913

Two modules require each other - corona

I have : "play.lua" and "menu.lua" and and it works perfect. in "menu.lua": local play = require('play'). I made a button that takes you back to menu, so i wrote in "play.lua": local menu = require('menu') and that shows me error. and when I require just 'menu' in play.lua every thing is ok? so what is the problem? I can't two modules to require each other?

Upvotes: 1

Views: 717

Answers (1)

Paul Kulchenko
Paul Kulchenko

Reputation: 26744

You can's have two modules that require each other; if you try that you are likely to get loop or previous error loading module 'X' error. The situation is the same as you get with recursive functions when a calls b, which calls a: the recursion requires some stopping criteria, otherwise it will continue indefinitely. Lua authors implemented a check to detect this situation for require and generate the error you see.

You need to restructure your code to avoid this. You can, for example, extract shared code from play into core and instead of requiring play from menu, require core from both of them, which will eliminate the issue.

Upvotes: 2

Related Questions