FAQ Wednesday: Lua Programming Tips

Frequently Asked Questions
Share on Facebook0Share on Google+2Tweet about this on TwitterShare on LinkedIn0

Frequently Asked Questions

It’s Wednesday and time for another FAQ session. Here are some frequently asked questions (FAQ) about coding in Lua. This FAQ coves basic Lua language programming and focuses on some of the features that are unique to the Lua language.

1. Do I need to end Corona code lines with “;”?

You may see Corona (Lua) code written like this

And at times it’s written like this

The difference is the ending semicolon (“;”) character. Many languages (JavaScript, C++, ObjectiveC, etc.) require the semicolon to indicate the end of line. Lua does not require the semicolon because it can figure out where the statement ends. Having the semicolon character at the end of the statement doesn’t hurt, but it’s not required.

Sometimes you will see multiple statements on a line using the “;” to separate the statements:

But in Lua, this does the same thing (without the “;”):

2. Can Lua functions return multiple values?

A feature that is pretty unique to Lua is the ability to return multiple values from a function. Look at the following example:

If the connection fails, it returns false and a string indicating the problem. If the connection is good, it only returns true (the “reason” variable will contain nil).

3. Can you assign multiple values to multiple variables?

Another handy feature of Lua that was demonstrated in the last example – the ability to assign multiple variable at the same time.

This is the same as:

It really doesn’t matter which method you choose, but sometimes the first method improves readability of your code.

4. Are parentheses needed in “if” statements?

Some languages require the conditional statement to be enclosed in parentheses.

In Lua, the parentheses are optional and only required to ensure that the statement is evaluated correctly. Lua requires the “then” statement to indicate the end of the conditional.

5. How do I add Block Comments to my code?

Block comments is good for documenting your code over multiple lines or disabling a section of code to try out new code or for debugging purposes. The real advantage of using block comments instead of commenting out each line with “––“ is when you have a number of comment lines together. Here is how it’s done:

The comment start with “––[[“ and ends with ”––]]”. The useful thing about commenting out code in this way is you can re-enable the code by adding an extra “-” to "––[[", as shown below:

The above code would execute because of the “–––[[“ (three dashes instead of two).

That’s it for today’s questions. I hope you enjoyed it and even learned a few things.

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

This entry has 15 replies

  1. Very great FAQ this week! Keep them coming!

  2. ojnab says:

    third tip is not completely correct. This is better:

    local red, green, blue = 100, 255, 128

    is the same as:

    local red = 100
    local green = 255
    local blue = 128

    • Tom Newman says:

      @ojnab, you’re right and I added the “local” statements.

  3. Richard says:

    Wait, –[[ –]]? Really? All this time I’ve been using –[[ ]] and it worked just fine…?

    • Tom Newman says:


      You’re right that “]]” does work if you only want to comment out the block. Where it doesn’t work is when you use “—[[” to uncomment the block. Lua tries to execute the “]]” line and stops with an error. I always use “–]]” to end my comment block because I use block comments to help in debugging my code.

      • J. A. Whye says:

        Typo in your comment – you say you always use “-]]” but I think you mean –]]

        And in the associated tip above you left out a dash in this spot:
        …by adding an extra โ€œ-โ€ to “โ€“[[“, as shown below:


        PS – For people who think I’m being too picky since they are just typos, I’ve spent time tracking down problems that resulted from me copying code that contained typos. I don’t care about spelling errors, bad grammar, etc., as long as it’s NOT in code fragments. ๐Ÿ™‚

        • J. A. Whye says:

          Ah-ha! Maybe YOU didn’t make the typo. Looks like the blog is stripping out one of the dashes — or combining two dashes into a emdash because it thinks it knows what we mean.

          There should be two dashes here — were there?
          There should be two dashes here –]] were there?


          • Tom Newman says:

            @Jay, yes WordPress was changing the dashes on me in the text body. The code snippets was correct and unchanged. I modified the post to try an override what WordPress is doing.

            This “dash” issue was causing problems on the FAQ where I posted the file name iOS needed to enable “tall” mode for iPhone 5. I think if you copy the code from the snippets and not the blog body, it should be correct.

  4. V says:

    “functions return multiple values – a feature that is pretty unique to Lua”

    Really? I must have lived under the rock then. Python support it, Perl, Ruby as well. What’s unique about it? I guess that pretty much any dynamically typed language is capable of it…

    • Tom Newman says:

      @V, my comment was in the content of languages that are typically used to create mobile apps: C++, ObjectiveC, and Java. I haven’t used Python, Perl or Ruby to create an iOS or Android app.

  5. GreenCastle says:

    Good article. I’d point out to the reader that just because you CAN do multiple assignments, doesn’t mean it is stylistically the best thing.

    For variables such as red, green, blue whose data clearly have complimentary roles (people are used to thinking about RGB as one value) it is good, but I’ve seen modules where the author assigns 8-9 values on one line that have nothing to do with each other and it’s a pain to read.

    • I’m with GreenCastle on that one. Readability of code is more important than the number of lines or how terse you can make your statements. Especially if someone else is going to be looking at your code later on.

  6. And another thing:
    Why do we need to put in our details when responding to a blog post. I’m already logged into the coronalabs site.

  7. JF says:

    One of the things I really miss from the other mentioned languages is:

    a += b, a -= b, etc.

  8. Crossman says:

    Thank you! Even for someone who’s been using Lua for a long time these basic tutorials are helpful in understanding some things.