two modules require each other

Are you using Composer? That will make it much easier to move between screens.

http://docs.coronalabs.com/api/library/composer/index.html

I am pretty newbie. plaese can you explaine me more?

A composer scene is a lua module, which makes use of some functions built into Corona for managing and transitioning between scenes.

The code shown on that link I posted is a blank composer scene. You put your code to draw objects in scene:create, making sure all are inserted to ‘group’.

Then, if you are in menu.lua, the code:

[lua]

composer.gotoScene(“play”)

[/lua]

Will take you to play.lua, and will also clean up any display objects created in menu.lua (so long as they were inserted into ‘group’.

This explain things in more detail:

https://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

you cant just tell me how to make the to modules requried?

but thanx anyway

I’m not sure it’s even possible that way, in any case a little time to learn Composer will benefit you massively in the long term. Just read the tutorial slowly and carefully - you have to walk before you can run! I was in your position 3-4 years ago, we all have to learn the basics sometime.

@idanahal3,

Try this:

module1.lua

--module 1 local myModule local module2 = require 'module2' module2.link( myModule ) myModule.doit1 = function() print("doit1 in module 1") module2.doit1() end myModule.doit2 = function() print("doit2 in module 1") end return myModule

module2.lua

--module 2 local myModule local module1 myModule.link = function( module ) module1 = module end myModule.doit1= function() print("doit1 in module 2") end myModule.doit2= function() print("doit2 in module 2") module1.doit() end return myModule

main.lua

local module1 = require "module1" local module2 = require "module2" module1.doit1() print("--------------------") module2.doit1() print("--------------------") module1.doit2() print("--------------------") module2.doit2() print("--------------------")

I use the following in several parts of my code:

--- Helper to deal with circular module require situations. Provided module access is not -- needed immediately (in particular, it can wait until the requiring module has loaded), -- the lazy-required module looks like and may be treated as a normal module. -- @string name Module name, as passed to @{require}. -- @treturn table Module proxy, to be accessed like the module proper. function LazyRequire (name) local mod return setmetatable({}, { \_\_index = function(\_, k) mod = mod or require(name) return mod[k] end }) end

Then in module1 :

local module2 = LazyRequire("module2") -- custom require local M = {} -- ... stuff function M.DoSomethingCool () module2.AwesomeFunction() -- module2 is require()'d on first access end function M.Init () -- get things in order! end -- ... more stuff return M

and in module2:

local M = {} local module1 = require("module1") -- n.b. normal require module1.Init() -- other stuff... function M.AwesomeFunction () -- awesomeness end return M

This works quite well when one of the modules will only need the other down the road, and not in the main chunk.

@StarCrunch,

I love it.  That is definitely going in my toolbox.

-Ed

Thanks both

We just posted a tutorial with a sample complete game template.   It’s got a full composer based set of Lua files you can use to start building your game:

https://coronalabs.com/blog/2015/04/14/tutorial-the-basic-game-template/

Rob

Are you using Composer? That will make it much easier to move between screens.

http://docs.coronalabs.com/api/library/composer/index.html

I am pretty newbie. plaese can you explaine me more?

A composer scene is a lua module, which makes use of some functions built into Corona for managing and transitioning between scenes.

The code shown on that link I posted is a blank composer scene. You put your code to draw objects in scene:create, making sure all are inserted to ‘group’.

Then, if you are in menu.lua, the code:

[lua]

composer.gotoScene(“play”)

[/lua]

Will take you to play.lua, and will also clean up any display objects created in menu.lua (so long as they were inserted into ‘group’.

This explain things in more detail:

https://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

you cant just tell me how to make the to modules requried?

but thanx anyway

I’m not sure it’s even possible that way, in any case a little time to learn Composer will benefit you massively in the long term. Just read the tutorial slowly and carefully - you have to walk before you can run! I was in your position 3-4 years ago, we all have to learn the basics sometime.

@idanahal3,

Try this:

module1.lua

--module 1 local myModule local module2 = require 'module2' module2.link( myModule ) myModule.doit1 = function() print("doit1 in module 1") module2.doit1() end myModule.doit2 = function() print("doit2 in module 1") end return myModule

module2.lua

--module 2 local myModule local module1 myModule.link = function( module ) module1 = module end myModule.doit1= function() print("doit1 in module 2") end myModule.doit2= function() print("doit2 in module 2") module1.doit() end return myModule

main.lua

local module1 = require "module1" local module2 = require "module2" module1.doit1() print("--------------------") module2.doit1() print("--------------------") module1.doit2() print("--------------------") module2.doit2() print("--------------------")

I use the following in several parts of my code:

--- Helper to deal with circular module require situations. Provided module access is not -- needed immediately (in particular, it can wait until the requiring module has loaded), -- the lazy-required module looks like and may be treated as a normal module. -- @string name Module name, as passed to @{require}. -- @treturn table Module proxy, to be accessed like the module proper. function LazyRequire (name) local mod return setmetatable({}, { \_\_index = function(\_, k) mod = mod or require(name) return mod[k] end }) end

Then in module1 :

local module2 = LazyRequire("module2") -- custom require local M = {} -- ... stuff function M.DoSomethingCool () module2.AwesomeFunction() -- module2 is require()'d on first access end function M.Init () -- get things in order! end -- ... more stuff return M

and in module2:

local M = {} local module1 = require("module1") -- n.b. normal require module1.Init() -- other stuff... function M.AwesomeFunction () -- awesomeness end return M

This works quite well when one of the modules will only need the other down the road, and not in the main chunk.

@StarCrunch,

I love it.  That is definitely going in my toolbox.

-Ed

Thanks both

We just posted a tutorial with a sample complete game template.   It’s got a full composer based set of Lua files you can use to start building your game:

https://coronalabs.com/blog/2015/04/14/tutorial-the-basic-game-template/

Rob