help with module

hey, i was reading the code for an example corona game named “corona cannon master”. There is a lua file for each level e.g (1.lua, 2.lua, 3.lua). in those files, there are only return statements in them, e.g

return { map = 1 cannon = {...} } 

they have a game.lua file which is the scene where the game is actually played, i was wondering if someone can show me how this works. Getting info from a file and creating things from it. Thanks

Oppong,

After ‘very briefly’ looking at the sample code, it appears the level_select scene calls the reload_game scene.

When reload_game is called, level_select sends in the ‘level id number’ as an argument/params.

reload_game scene then calls game scene.

When game scene is called, reload_game just passes on that ‘level id number’ as an argument/params to game scene.

So for each level, game scene is called and totally re-created.  It is re-creeated with most of the same information, except the level information that is pulled in from 1.lua, 2.lua, 3.lua etc…  

That table is returned to game.lua with this line of code:

self.level = require(‘levels.’ … self.levelId)

self.level now holds that table information that was in the level file (1.lua, 2.lua etc…)

That table holds the different data that game scene needs to know for the specific level.  Everything else game scene uses to layout the particular level is reproduced each time the game scene is called, with the specific positioning and number of bugs and boxes and such for the present level.

You should understand tables at this point.  If you are not clear on tables, look on internet for lua tutorials or other corona tutorials on tables.  They are used a lot in corona/lua programming.

The author of that sample code, could have coded a large table in the game.lua file instead of making all those separate level files.

Maybe he/she would name the table ‘levels’, and nest each level table as a sub table.  

BUT, having them as separate modules is preferred by most developers. It keeps the code clean, makes it easier to debug things, and easier to expand and reuse code.

As I said, i briefly looked at the sample code, so i could have missed something, but the principals still work the same.  Module type coding helps keep the code clean and easy to work with.

Hope this helps.

Bob

Oppong,

After ‘very briefly’ looking at the sample code, it appears the level_select scene calls the reload_game scene.

When reload_game is called, level_select sends in the ‘level id number’ as an argument/params.

reload_game scene then calls game scene.

When game scene is called, reload_game just passes on that ‘level id number’ as an argument/params to game scene.

So for each level, game scene is called and totally re-created.  It is re-creeated with most of the same information, except the level information that is pulled in from 1.lua, 2.lua, 3.lua etc…  

That table is returned to game.lua with this line of code:

self.level = require(‘levels.’ … self.levelId)

self.level now holds that table information that was in the level file (1.lua, 2.lua etc…)

That table holds the different data that game scene needs to know for the specific level.  Everything else game scene uses to layout the particular level is reproduced each time the game scene is called, with the specific positioning and number of bugs and boxes and such for the present level.

You should understand tables at this point.  If you are not clear on tables, look on internet for lua tutorials or other corona tutorials on tables.  They are used a lot in corona/lua programming.

The author of that sample code, could have coded a large table in the game.lua file instead of making all those separate level files.

Maybe he/she would name the table ‘levels’, and nest each level table as a sub table.  

BUT, having them as separate modules is preferred by most developers. It keeps the code clean, makes it easier to debug things, and easier to expand and reuse code.

As I said, i briefly looked at the sample code, so i could have missed something, but the principals still work the same.  Module type coding helps keep the code clean and easy to work with.

Hope this helps.

Bob