Posted on by

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

local myRect = display.newRect( 0, 0, 100, 50 );

And at times it’s written like this

local myRect = display.newRect( 0, 0, 100, 50 )

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:

a = 100; b = 200

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

a = 100 b = 200

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:

local function testConnection()
    if not noConnection then
        return false, "Connection Failed!"
    else
        return true
    end
end

local result, reason = testConnection()
print( result, reason )

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.

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

This is the same as:

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

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.

-- C code
if ( a > b) {
     ...
}

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.

-- Lua code
if a > b then
    ...
end 

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:

--[[
local function testConnection()
 if not noConnection then
 return false, "Connection Failed!"
 else
 return true
 end
end
--]]

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:

---[[
local function testConnection()
 if not noConnection then
 return false, "Connection Failed!"
 else
 return true
 end
end
--]]

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.


Posted by . Thanks for reading...

15 Responses to “FAQ Wednesday: Lua Programming Tips”

  1. ojnab

    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

    Reply
  2. Richard

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

    Reply
    • Tom Newman

      @Richard,

      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.

      Reply
      • J. A. Whye

        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:

        Jay

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

        Reply
        • J. A. Whye

          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?

          Jay

          Reply
          • Tom Newman

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

  3. V

    “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…

    Reply
    • Tom Newman

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

      Reply
  4. GreenCastle

    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.

    Reply
    • WideAwakeGames

      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.

      Reply
  5. Crossman

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

    Reply

Leave a Reply

  • (Will Not Be Published)