NOTE: This tutorial is outdated. Please refer to the more current External Modules in Corona tutorial.
  1. Oskar Persson says:

    Thank You!

    I had not noticed the earlier tutorial for this and this was just was I needed for my current (and epic ;-) ) project.


  2. Thanks Mr Beebe! This is very helpful having some further explanations on why things work the way they do. Normally all we get is “This does this” but not the “why this does this.” So I’m grateful for your blogging efforts.

  3. I’ve been advised to stay away from the module function.

    Instead I tend to use a format of:

    local M={}

    -- manually shift external stuff I want into local variables.
    -- not actually required if you just copy _G into what we're about to do,
    -- but a matter of personal preference
    local display=display


    [[ fields and functions ]]

    return M

  4. Might be worth showing how some basic object oriented programming principles can be applied to modules as well. This allows you to think in objects like balloons or rockets.

    For example put this in file called Balloon.lua. And you have means to create balloons each with it’s own state while reusing the same logic.

    module(…, package.seeall)

    function new (color)

    local balloon = {}

    balloon.color = color
    balloon.type = “round balloon”
    balloon.state = “full”

    function balloon:printInfo()
    local freeMem = collectgarbage(‘count’);
    print(“GC Count : ” .. freeMem/1024 .. “MB”)

    function balloon:pop()
    if self.state == “full” then
    self.state = “popped”
    freeMem = collectgarbage(‘count’);
    print(“GC Count : ” .. freeMem/1024 .. “MB”)
    print(gcinfo ())

    return balloon


    –Here is the main.lua file.
    local BalloonFactory = require(“Balloon”)

    –Create two instance of the balloon.
    local b1 =“RED”)
    local b2 =“BLUE”)

  5. Jon, thanks for this.

    I suppose variables can also be referenced from these external modules. (like properties). if there were a variable in the external module, can it also be called as exmaple.varname based on your example?

  6. Jonathan Beebe says:

    @Altaf: Yup, you’re exactly right :-)

    @Everyone else: Thanks for all your comments, input, and suggestions!

  7. Thanks for the great tutorial.
    Should you cache other global functions (such as the math functions) before using it withing your module function? In other words, does caching a function locally cache every other function called internally within it?

    • Jonathan Beebe says:

      When you cache a function, to a certain extent it does cache functions within that function (because look-up is reduced), but as a rule of thumb you should try to localize (e.g. cache) as many functions/variables as you possibly can while still maintaining the functionality of your app.

  8. Thanks for the great tutorial Jon.
    What was a bit confusing for me when starting to work with my own classes in corona, was calling a function like a variable, ie example.helloWorld, and not example:helloWorld(), until i found out, that the dot (.) is calling a class method and the colon(:) is calling an object method. i read somewhere, that in lua, the colon is just a shortcut to the dot call, without the need to pass the object itself, so it can be accessed via “self” in the function…
    maybe this is of interest for anyone…

  9. I guess there is something I don’t quite understand still about modules. I see the benefit but for the life of me I have never managed to get one module see another module.

    For example lets say I have a particle module and I also have another module to do with something else along side main.lua. At times the particle module needs some information from other module yet nothing I do can get them to see each other.

    There is never any issue for main.lua seeing and calling functions in each of the other modules but the modules can never seem to see each other.

    Any ideas on this kind of issue?

    Thank you for your clear and to the point articles.

  10. @Mike R – all you have to do is call the require() function within the other external module. Calling require in main does not make it accessable to all other modules, only accessable to main.

    @Jonathan Beebe or anyone else… My question is this: is there a difference between caching a function locally (as in your example) as opposed to only localizing the value/object the function returns (as in Bob’s example)?

    If so what is the benefit?

    Should the function be localized everytime you use it, (as in if I use it within another function that is in the same scope?). Or should I only localize it once at the top level, and then use it within that scope?

  11. What about this:

    timer.performWithDelay( 250, doBellyDance )

    But sometimes you want to utilize .performWithDelay and you want to pass parameters to that function specified. How do you accomplish this?

    • Jonathan Beebe says:

      Hi Jerome, you can do it like this:

      timer.performWithDelay( 250, function() doBellyDance( arg1, arg2 ); end )

      Or, a little more code but a more recommended method:

      local callBellyDance = function()
      doBellyDance( arg1, arg2 )

      timer.performWithDelay( 250, callBellyDance )

  12. @Jonathan Beebe:
    Whats the difference between “local table functions” vs “local functions”???
    So, your function would be a “local table function”, because you assigned a function to a local variable, right? Or here I have another one:

    local var = function()
    --do here
    var() --call

    Here I have a normal local function:

    local function var()
    --do here

    What are differences? (Dis)Advantages?

  13. how would i pass a local variable(a table from main.lua) to an external module
    and then manipulate this table from inside the external module
    and then pass that back to the main.lua as a table.

    And then how to access the above table from the external module
    and then perform any cleanup/garbage collection.

    I know thats a lot of “and thens” but this is what holding me back from
    producing a quality “built/designed” program.

  14. Great ! but if I only want to include code ? No use of function. I just need to have some piece of code outside my main.lua, and i want to include it where I require to be loaded.

  15. I am not sure to make a difference beetween a call like

    myImage = muModule:placeMyImage()


    myImage = muModule.placeMyImage()

    All work.

  16. Who is the handling of variable between main.lua und an Extern Module?
    In the mail.lua define a variable
    xyz = 5

    in the extern module I modify the value
    xyz = xyz – 1
    print(xyz) –output now 4

    back in the main.lua, the variable show again 5
    is this correct ? and Why?

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>