Posted on by

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!


Posted by . Thanks for reading...

21 Responses to “Daily Build Feature: Lua Files in Folders”

  1. J. A. Whye

    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

    Reply
  2. Jonathan Beebe

    @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

    Reply
  3. J. A. Whye

    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

    Reply
  4. Darren Osadchuk

    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”)

    Reply
  5. Ken Ibrahim

    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.

    Reply
  6. Lance

    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.

    Reply
  7. Graham Ranson

    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.

    Reply
  8. Tom

    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.

    Reply
  9. Matthew

    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.

    Reply
  10. Mario Roberti

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

    Reply
  11. Tom (Ansca Staff)

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

    Reply
  12. Fran

    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

    Reply

Leave a Reply

  • (Will Not Be Published)