Modules loaded in modules question

I feel like such a N00b and I should know this already.

FYI, I'm using Director for a multi-level app. On each of the play levels, I want to have a control panel display. The functions of the control panel I want to be in their own modules. These sub-modules will be storing and retrieving data that needs to persist between levels.

So what I want to do is have:

controlPanel = require("controlpanel")

which will return a display group that has the control panel, its buttons and such. Those buttons will cause different objects to show on the screen. Those objects will need their own lua files to manage showing and hiding the items, adding data to and retrieving data from the objects.

So if inside controlpanel.lua, I do:

objectA = require("objectA")
objectB = require("objectB")

Then from level1.lua and level2.lua I do the require to load the control panel, can I access objectA and objectB?

Or do I have to from level1.lua load all three files, then in level2.lua also load all three files?

Any time you load a module (the first time from anywhere), it is accessible as a branch of lua's root: eg _G[moduleName]

You don't normally need _G unless you've not used package.seeall in an enclosing module definition and even then you need to hold some reference to the global environment or it won't work

With all of that said, it's not clear from your question that:
require("objectA")
is actually a true Lua module, or just some chunk you are loading...a HUGE difference

but so long as you don't declare "ObjectA" as local, then it should be always visible under:
controlPanel.ObjectA
after the first time controlPanel is loaded

views:1454 update:2011/9/26 8:07:09
corona forums © 2003-2011