Daily Build Feature: Lua Files in Folders

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0

Corona Daily Build Feature October 6, 2011In case you missed it, as of Daily Build 619, subscribers can now place Lua scripts (with an exception of main.lua) in sub-folders within their project directory for iOS apps (doesn’t apply to Android apps just yet).

There’s a few important things to note, however. When using the require() function in Lua, slashes (“/”) are not supported when accessing files in sub-directories (as with usual file operations). Instead, you use a “.” to signify a directory level. This also means you cannot have a “.” in your module name.

Here’s an example of requiring “bar.lua” from a sub-folder named “foo” that’s located in your app’s project directory (aka “resource directory”):

local fooBar = require( "foo.bar" )

Once again, bar.lua, is placed in a folder named “foo”, so here’s what the directory structure might look like for the example above:

CoronaProject (folder)
foo (folder)

Once again, this currently only works for iOS apps, so all of your resource files still need to remain at the top-level for Android apps. Also, this feature requires—at minimum—build 2011.619, which only subscribers have access to at the moment via Daily Builds.

Subscribe today and get the latest features and bug fixes right way!

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0

This entry has 21 replies

  1. Lance says:

    Yes!! Thank you ansca! Now I can organize a bit.

  2. J. A. Whye says:

    I guess I’ll be the whiner baby here…

    Why would you guys use a period instead of a slash? Slash is what makes sense to developers because that’s what everything else uses — if you need to change the slash to a period internally when compiling, no big deal. But why make the developers change their way of thinking when the computer can do it for them?


  3. Jonathan Beebe says:

    @J.A. Whye: That’s how the require() function in Lua worksโ€”it uses a period instead of a slash.

    From Programming in Lua, section 8.1:

    …ANSI C (the abstract platform where Lua runs) does not have the concept of directories.

    Read more here:

  4. J. A. Whye says:

    So the creator of Lua made a mistake in doing something non-standard (in my opinion at this point). I’m not sure that’s a good reason to continue it, although I can understand the desire to NOT change the way Lua works under Corona SDK.

    Still, the reason for using Corona SDK is to make development faster and easier. You could create an additional way to require files that allows a slash:

    local fooBar = include( “foo/bar” )

    …although in that case it’s still not obvious to the developer — they’d have to look up that there’s an include() keyword and if they’re going to have to look up something, I guess it’s just as easy to look up that dot is the folder separator.

    But it still seems wrong. ๐Ÿ™‚


  5. I don’t agree that the dot is necessarily non-standard (e.g. it’s used in ActionScript and JavaScript for packages/classes), but rather than argue about it:

    local function include(mod)
    mod = string.gsub(mod, “/”, “.”)
    return require(mod)

    local bar = include(“foo/bar”)

  6. Ken Ibrahim says:

    The “.” syntax mirrors that of Python which I use quite a bit and so is very familiar. I don’t see this as a problem. Just my 2 cents.

  7. Lance says:

    I was going to suggest regex, but @Darren beat me to it.

  8. Lance says:

    I suppose that is actually just direct replacement. You don’t really need an expression. It would be fun to make a library for handling modules with this included.

  9. Horace Ho says:

    Can we put .png files in sub-folders?

  10. I’ve been publishing Android apps with lua files in subdirectories without issue.

  11. To slightly modify Darrens code you could also hijack the require function:

    require = function( mod )

    if not _G.cachedRequire then
    _G.cachedRequire = require

    mod = string.gsub(mod, โ€œ/โ€, โ€œ.โ€)

    return _G.cachedRequire( mod )


    Although I haven’t actually tested this yet.

  12. Tom says:

    The change starting in build 619 means you can not use files with embedded “dots” in the file name. For example, you can no longer name your Lua files: “level.one.lua”. No work-around code in Lua will overcome that because Corona interprets the “dot” as a subdirectory.

  13. bktv says:

    can graphic files also be organized into subdirectories this way?

  14. MarkHenryC says:

    A pity alias’s aren’t resolved so common lua files can be kept in a library. Is there another way to do this?

  15. Donny says:

    Thanks for this feature!! This makes modular programming easier.

  16. Matthew says:

    I am wondering if the plan is to support this on all platforms? My guess is yes. I would like to know that I can build for at least iOS and Android with files in folders. @codeRebelBase says that he has been doing this without issue. Are you saying you built and ran on device successfully, or just in the simulator? From everything I have read you have to run a python script that one of the developers posted on the forums that will allow you to build for the devices.

  17. Andrew says:

    Can anyone confirm if this is working in Android now?

  18. @Andrew

    Works on my Samsung Galaxy S2

  19. @Andrew Doesn’t work on my Kindle Fire deploys. However, works fine in the simulator. Poop! I spent 2 days trying to figure this one out. Oh well, I can live with this for now. ๐Ÿ™‚ I’ve tried a couple version of the daily builds too, from yesterday’s to March 29th’s and it still chokes when I do something like: local Ship = require( “LibrariesSmall/Ship”)

    my stupid app says “Your application has become corrupted” or something equally informative. ๐Ÿ™ But like I said, keep the LUA files in your root directory and then you’re good to go.

  20. The Corona simulator allows “/” to separate folder and file names but it doesn’t work on the device. You need to use a “.” between the folder and file name. The above example should be: Ship = require(“LibrariesSmall.ship”)
    That works on both the Corona Simulator and device (iOS and Android/Fire).

  21. Fran says:

    Tom, what about image files??? I haven’t found any way of structure images in folders during the development phase… It’s true that I had to create a build once everything worked in order to delete the folders. But the development process was easier.

    Is there any way of structuring image files into directories? Some other users asked you in this post and many others in the forums, but no answer was given….