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)
bar.lua
config.lua
build.settings
main.lua

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!

  1. 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?

    Jay

  2. 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:
    http://www.lua.org/pil/8.1.html

  3. 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. :)

    Jay

  4. 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)
    end

    local bar = include(“foo/bar”)

  5. 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.

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

    require = function( mod )

    if not _G.cachedRequire then
    _G.cachedRequire = require
    end

    mod = string.gsub(mod, “/”, “.”)

    return _G.cachedRequire( mod )

    end

    Although I haven’t actually tested this yet.

  7. 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.

  8. 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.

  9. @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.

  10. 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).

  11. 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….

    Regards

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>