loading modules from sub directory / folder

Is there a way to load modules from sub directory / folder?

Using [lua]require( “_levels/level01” )[/lua] works on the simulator but fails on device (iPhone 3G)…

Is it possible? If so, what is the syntax?

Thanks,
EZ [import]uid: 9536 topic_id: 6931 reply_id: 306931[/import]

At the end of the file “level01”, place this code:

return _M [import]uid: 6175 topic_id: 6931 reply_id: 24273[/import]

Unfortunately, unless I’m doing something wrong, that doesn’t make a difference…
It works on the simulator, fails on device…

Using iPhone 3G, Corona version 2010.243 (2010.12.2)

Here is a sample test code (you’ll notice that the app fails before box_sprt is being created):

main.lua
[lua]-- bck_sprt
local bck_sprt = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
bck_sprt:setFillColor( 192, 255, 64 )

– load level
require( “_levels/level01” )

– box_sprt
local box_sprt = display.newRect( 0, 0, 0.5 * display.contentWidth, 0.5 * display.contentHeight )
box_sprt:setFillColor( 255, 64, 64 )
box_sprt.x = 0.5 * display.contentWidth
box_sprt.y = 0.5 * display.contentHeight[/lua]

_levels/level01.lua
[lua]module( …, package.seeall )

print(“level01”)

return _M[/lua] [import]uid: 9536 topic_id: 6931 reply_id: 24394[/import]

I’m not certain of when (which version) Corona added support for folders, but 243 is pretty old and so you’d better check with them. In fact, I’ve never seen folder support formally documented, just mentioned by other developers.

Just keep in mind that require returns a value from package.loaded, and module creates a value in package.loaded, but if the names don’t match, then required returns a boolean instead of the module without “return _M” at the end. In your case, you are not passing the value from required back to anything, and your code is not referencing the namespace, so it’s a moot point, but in a real-use situation, it’s something to watch for.

[import]uid: 6175 topic_id: 6931 reply_id: 24424[/import]