Simple introduction to LUA and Corona SDK part 1

When someones asks me what should they do to get into the mobile business, I can’t help but point them to the Corona SDK (http://www.coronalabs.com).
Compared to other SDKs, it strips away all the painful processes of developing an app while still providing a full and flexible experience. It leaves room for creativity, allowing you to focus just on making your product shine.

For novices it can possibly be the best starting point, especially for those who get scared just hearing about “code”. What makes all of this possible, among the other things, is a simple and easy scripting language, LUA.

But before telling you why LUA is so great, we need to talk about the “basics” of coding and LUA itself. Mainly variables, tables and functions.

Variables are like boxes. Like in real life, you can put any kind of stuff you like in your boxes, and then you can put a tag on them so it’ll be easier for you to know what’s inside them.
A variable is just the same. You give it a name, and you put something in it. At any time, you can look up that name to check its content once again, change it, or use it for something else.

Here’s a bit of code that shows how to declare a variable (create your box) and put a number into it.

Let’s analyze this line a bit. First we have the word “local”. We don’t need to know what exactly it does at this point, but just keep in mind that when you declare a variable (and only there), it is needed. Then, we have the variable name, our tag, “myFirstVariable”. This name identifies our variable, and can be anything you want. You can put numbers in it or underscores.
Then there’s the = sign. Like in math, it tells us that our variable “equals” to something. It’s basically our way to say that we want to fill the box. Then comes our content, the number 1. It could’ve been texts, other numbers, or even other variables. For last, the “;”. This little guy tells us that we’ve finished writing our line. It’s not strictly required, but I think it makes the code more readable.

We can do quite a bit of stuff with our variables. Here’s an example.

As you can see, we only have “local” once, at the very first line. It means we’re declaring a new variable. Then, since we’ve already declared it, we don’t need to write it anymore.
The second line shows how you can change its content, by simply writing what you’d like there to be. The word “hello” is a string (it’s defined by the ” ” ). Then we change its content again, this time with a simple operation. 1+1=2, so now the content of myFirstVariable is 2.
For last, we use our variable to do another operation and “update” its content. Since it contained 2 before, 2+2=4.

Now, imagine having tens, hundreds of variables. Maybe you need to operate on them all at the same time, or you’d just like to have an easier way to access them without remembering each tag you’ve used. Here comes tables. Tables are a LUA “specialty”. If variables are like boxes, tables are like shelves. You can use them to store your boxes, as many as you want, and retrieve them through their position in the shelf, or through another tag.

As you can see, the declaration is exactly the same of a variable (we’re actually creating a variable that will hold our table). The “{}” defines the table. Now we can start filling the tables with anything we want.

As you can see, by the use of “[]” we can define which spot of our table we want to address. It can be either a number or a string, and it can contain anything, from numbers, to strings, variables and even other tables.

Now that we know how to create a variable, and how to use tables, let’s take a look at a few basics statements: if, for and while.

“if” is one of the most useful and used statements. It basically allow you to put conditions for a certain part of your code to be executed. It also allows you to do something else, in case these conditions aren’t met.

This example should be pretty straightforward. If the content of myFirstVariable is 1, then we change its content to 3. Else, we change its content to 2. The “==” is an operator used for comparison, same as “>=” (greater or equal to), “<=” (lower or equal to) and “~=” (different from). They report either the value “true” or the value “false”.

The “for loop” statement allow you to loop the same snippet of code for a certain amount of times, while being able to keep track of which iteration you’re in.

We’re creating a table, using a “for loop” to fill it with the numbers from 1 to 10. The “i” is our declared variable that will keep the number of the current iteration we’re in. It goes from 1, to 10, increasing of 1 for every iteration (so it will iterate 10 times). Then we pass it to our table, using the “[]”. It’s actually like writing “[1]”, “[2]”, “[3]” and so on all the way to 10. For last, we use the “i” itself as a content for our spot in the table. Since “i” goes from 1 to 10, it’ll feel every spot with a different number from 1 to 10 depending on the iteration.

The “while loop” is similar, but it can potentially be endless. We specify a condition, and until that condition is proven “false”, the loop will keep going.

Our variable starts at 0. The condition states that the loop must go on as long as myFirstVariable contains a number lower than 10. Inside the loop, the code we run at each iteration sums up myFirstVariable with 1. This means that at every iteration, myFirstVariable increases of 1. It’ll take 10 full iterations for the loop to stop.

The last and possibly more important aspect we’re going to cover in this tutorial, are functions.
Functions are basically snippets of code that you can run at will, by simply calling them any time you wish.

Let’s analyze this in more detail. First of all, you can immediately see that we’re declaring a variable, containing a function. myFirstFunction is the name of our variable (and so, of our function). The “function()” is actually the declaration of the function itself. The code that is written between it, all the way to the word “end” is the code that will be executed every time the function gets called.
The values number1 and number2 are actually parameters that you can “pass” to your function. This means that when you call it, if you put two values between the “()”, the code in your function will be able to use them, specifically as number1 and number2 variables.
Inside the function you can see the word “return”. It means exactly what it says. It’ll return anything you like. In this case, it will return the variable result, which is the sum of number1 and number2.
Below we start calling our function. Every time we do so, the 2 numbers we’re passing it get summed up, and the result goes straight back to us, filling our variables.
This means that firstResult, once the function has executed, will contain 4, secondResult 9, and thirdResult 11.

This covers all the basic syntax and concepts you should need to tackle the development of your app. In part 2, we’re going to use Corona SDK to build our first scene, using everything we’ve learned here and just a bit more.

For a more in-depth and thorough explanation, you can check out the LUA Reference Manual directly here -> http://www.lua.org/manual/5.1/

For questions, doubts and requests, you can comment directly here or Contact Us !

1 Comment

  1. Awesome… waiting for part 2. ;)

    Would you consider doing a post of things you’ve learn to code better, like how you choose design patterns in Lua/Corona, and things to avoid. My biggest issue at the moment is structuring my code correctly. Like passing variables efficiently, understanding the flexibility of tables, how to think out the code before you write it.

    As procedural as Corona is, it also nurtures lazy coding and to novice game devs, really promotes slow learning to gaining better coding practices.

    Your team seams to solve, package and run through game code beautifully. Tips and learning you have learnt would be amazing.

    Cheers (apologies for the convoluted comment)