Posted on by
NOTE: This tutorial is outdated. Please refer to the more current External Modules in Corona tutorial.

Posted by . Thanks for reading...

24 Responses to “Using External Modules in Corona”

  1. Oskar Persson

    Thank You!

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

    Oskar

    Reply
  2. Josh

    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.

    Reply
  3. Pop

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

    Instead I tend to use a format of:

    local M={}
    modulename=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

    setfenv(1,M)

    [[ fields and functions ]]

    return M

    Reply
  4. Bob

    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()
    print(self.color)
    print(self.type)
    print(self.state)
    local freeMem = collectgarbage(‘count’);
    print(“GC Count : ” .. freeMem/1024 .. “MB”)
    end

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

    return balloon

    end

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

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

    Reply
  5. Altaf

    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?

    Reply
  6. Jonathan Beebe

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

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

    Reply
  7. amirfl

    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?

    Reply
    • Jonathan Beebe

      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.

      Reply
  8. P Mayer

    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…

    Reply
  9. Mike R

    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.

    Reply
  10. Keith

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

    Reply
  11. Altaf

    hi, what if the external function has parameters? what is the syntax to localise such functions?

    Reply
  12. Jerome82

    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?

    Reply
    • Jonathan Beebe

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

      timer.performWithDelay( 250, callBellyDance )

      Reply
  13. jack0088

    @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
    end
    var() --call

    Here I have a normal local function:

    local function var()
    --do here
    end
    var()

    What are differences? (Dis)Advantages?

    Reply
    • Jonathan Beebe

      @jack0088: There really is no difference, except one very small difference. Normal functions can call themselves from within the function, and table functions cannot (unless a forward declaration was specified above that function). See ‘Function Definitions’ in the Lua manual (section 2.5.9): http://www.lua.org/manual/5.1/manual.html#2.5.9

      Reply
  14. troy nall

    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.

    Reply
  15. FREDERIC

    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.

    Reply
  16. FREDERIC

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


    myImage = muModule:placeMyImage()

    and


    myImage = muModule.placeMyImage()

    All work.

    Reply
  17. Michael

    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?

    Reply
    • Michael

      found the solution, to get the value from a module if have to ask via the moduel

      modulename.variable

      =

      modulename.xyz

      Reply

Leave a Reply

  • (Will Not Be Published)