Making Apps with the Lua Programming Language and Corona SDK – Lesson 5

posted in: Tutorial | 0

Welcome to Lesson 5! In this lesson we will discuss how to use Lua Tables!

 

These are the most powerful data structure in the Lua programming language! In fact, Lua Tables are the only data structuring mechanism in Lua and they are used to represent things like:

  • Arrays
  • Symbolic tables
  • Sets
  • Records
  • Queues

.. and so on.

 


Overview Map: Lesson 5


 

But first let’s see where we are in the big picture.

 

Learn Lua and Corona Lesson 5 - Overview 1000px

 

In the above map you can see that we’ll be learning about Lua Tablesand again, you will also notice that we’ll be working within the file called “Main.lua”.

 




 

Create a New Project –

Similar to Lesson 4,  we will first create a new Project within Corona SDK.

 

Open up Corona SDK, and create a New Project – 

Learn Lua and Corona Lesson 1 - Workflow1

 

When the New Project windows comes up –

  • For Application Name: Call it something like “Lesson 5”
  • For Project Folder: Select a location on your computer where you want to store the project (or leave it as the default location)
  • For Project Template: Leave the radio button selected as “Blank”
  • Leave the Upright Screen Size to “Phone Preset”
  • Leave the Default Orientation radio button as “Upright”

 

 




 

 

Similar to Lesson 4, I don’t want you to worry about any other file except “main.lua“. This file where we’ll type all our code in this lesson.

 

Open up “Main.lua” –

 

 

Similar to Lesson 4…. First, open up “main.lua” in Sublime Text 3 (or whatever text editor you use). In Sublime Text 3, go to File and choose Open Folder.. as shown below. Then navigate to your project folder and choose ‘Select Folder.

 

Also, ensure that your Syntax highlighting settings are either set to Corona SDK Lua (preferred) or Lua. To find this setting in Sublime Text 3… in the menu navigate to View. Then choose Syntax and make sure Corona SDK Lua is selected.

 

What you should see within Main.lua is the following commented code –

 

Types of Lua Tables

 

With Lua tables comes great flexibility! You can store a variety of the standard Lua types like strings, numbers, booleans, functions…… and yes… even tables within your tables?! Tables essentially act like Objects and this ability gives Lua some OOP-like (object orientated programming) features.

 

Lua tables can be either Numerically Indexed or Associatively Indexed (which means they can can have “key-value” pairs). Don’t worry too much about the jargon at the moment… the difference will become clear as we work through some examples below.

 

Numerically Indexed Lua Tables

 

Numerically indexed means that you can access the entries within a Lua table by the number associated with its entry. These numbers start at 1, and do not start at 0 like languages such as C, Javascript and Python. I personally like this 1’s based indexing… since it just makes more sense.

 

A basic example of numerically indexed tables is a simple array of numbers.

All tables are defined by using the curly braces { }.

Let’s define a simple array of numbers type of table here –

 

We have defined a simple array with 6 number entries within it. If we want to access a particular entry in this array we can use it’s “numeric index” like the following –

 

If you run the code above, you should get the value 56 in the output console.

 

Now the term “numerically indexeddoes not mean that a Lua table can only have numbers. For example we can create a table that has both strings and numbers like the following –

 

If you run the code above, you should get the value 6 and also the string “John” in the output console.

 

Now, if you thought mixing strings along with numbers inside a table was a little strange, check this next one out. You can also define a function inside a table entry. Let’s make a table that has: numbers, strings and a function inside it (in this case it’s an “anonymous” function.. check out Lesson 4 if you don’t know what that is)-

 

So you can actually access and call this function by doing either of the following –

 

If you run the code above, you should see the string of text “I’m the 5th table index.. weird!” printed out in the console twice.

 

Okay… so we’ve covered that you can store strings, numbers and functions within Lua tables. But you can also store Tables within Tables……

We’ll create a 2 dimensions data structure with entries that are numbers only. This is also known as a Matrix. Let’s take a look –

 

Note: Since Lua doesn’t care about white space, I’ve purposely added each table entry on a new line.. since it’s easy to visually see what the 2D index value result will be next –

 

If you run the code above, you should see the values 8 and 5.

 

If you tried to simply access the first index of the “myMatrix” table above by using myMatrix[1], you could simply be referencing the first table within “myMatrix”. Try this –

 

 

If you run the code above, you should see the value 9.

 

Now if your table doesn’t have a “key” (which we’ll talk about next), then each object in a table (string, number, function or table) is numerically indexed. Combining all the code above we can see the size of each of these tables defined using the “hash” or “pound” operator with each numerically indexed table –

 

 

With the 4 print statements near the end you should see the following output in the console –

Size of myArray: 6” (a table that has 6 entries that are all numbers)

Size of myTable1: 6” (a table that has 6 entries… of which 3 are strings and 3 are numbers)

Size of myTable2: 5” (a table that has 5 entries… of which 2 are string, 2 are numbers and 1 function)

Size of myMatrix: 3” (a table that has 3 entries… of which each is a table itself)

 

If you wanted to find the size of the 2nd tables with the myMatrix table… you’d use –

 

 

Associatively Indexed Lua Tables

 

Associatively indexed means that you can access the entries within a Lua table by using a “key”. If you think of a table as an object then this is like an attribute of an object.

 

All tables are defined by using the curly braces { }.

In fact we can define an empty table and give it entries later (either numerically or associatively indexed entries), or define a table and give it entries right away.

 

When we talk about associatively indexed tables we are talking about “key” and “value” pairs. Lets define a couple of these tables here –

 

 

In the above code we defined 2 different associatively indexed tables called “myTabA” and “myTabB“.  If we wanted to get (and print to console) the value associated with each of the “name” keys for these tables, we’d do the following –

 

 

When you run the code above you should get the names “Tim” and “Lisa” printed to the output console.

 

“Dot” syntax with Associative Lua Tables

 

Alternatively, to get (and print to console) the value associated with each of the “name” keys for these tables above you could have use “dot” syntax, like the following –

 

Again…. when you run the code above you should get the names “Tim” and “Lisa” printed to the output console.

 

 

Setting and Getting with Lua Tables

 

In the previous example with “myTabA” we could have expanded out (made larger) this table by adding another associative “key – value” pair… later on in the code.

Let’s try it here –

 

With the last two lines of code, we added another 2 elements to “myTabA”. There were 4 key elements (name, age, height, sex)…. and then we added 2 more key elements (nationality, birth_year) for a total of 6 “key-value” pairs.

 

If you wanted to check the new size you might be inclined to try –

 

If you try the hash/pound operator (#) on an associatively indexed table (key-value pairs) like shown above.. you find the results is 0 in the output console. This is because the hash/pound operator (#) only works on numerically indexed table elements.

 

To find the size of an associatively indexed table there is a special for loop called an Iterator.. that we’ll cover in an upcoming lesson.

 

Mixed (Numerically+Associatively Indexed) Lua Tables

 

Now you might be thinking… can I mix and match these two types of indexed Lua tables?

 

Well… of course…

 

Lets make a simple mixed table –

 

 

 

As you can see in this case, we have 5 entries within our table called “myMixed“. The first 3 elements are numerically indexed, and the last 2 entries are associatively indexed. 

 

Let’s try to access these with the following –

 

 

You’ll notice that if you attempt to access the 4th element using a numerical index like myMixed[4] you will only get a Nil value printed out. This is because although there is a 4th element, it is not numerically indexed…. it is associatively index with the key “dog”. To access it you need to use either –

  • myMixed[“dog”]…….. or
  • myMixed.dog

 

As you can see Lua tables are really dynamic and that makes them really powerful in Lua!

 

You can make a crazy table like this –

  • Numerically indexed with a number, string and table of numbers…
  • Then associately indexed with a function
  • Then associatively indexed with a table of numbers

 

 

 

Now let’s access some of these elements by doing the following –

 

 

When you run this code you should get the following in the output console –

  • Jane
  • 18
  • 40
  • “I’m the 7th element”

 

 

Tables with our simple Corona SDK App

 

Now in Lesson 4, we created a function that makes a new Angry Face image appear on the screen every time we make a call to that function. Recall the function needed 4 fixed inputs which were the: x-position, y-position, scale and rotation.

The simple Corona SDK App looked like this –

 

 

 

But there was a limit with each Angry Face image created on the screen… we had no way to access a particular image in the future if we wanted to either change it or delete it. 

 

Let’s see how we can use numerically indexed Lua tables to help us solve that problem.

 

We’ll first make a small change to our Lua function by returning a reference to the created object back to the calling function (this is done on Line 29 below). Next, we’ll create an empty Table (this is done on Line 39 below) –

 

 

Next, let’s allocate 4 elements to the currently emtpy table “myTable“.

 

 

There will be not real change to the app at this stage. However, since we’ve stored each of the objects in our “myTable” table we can now access or delete each of the Angry Face objects.

 

Add the following code –

When you run the code, your app should now look like this –

 

 

 

Next, let’s check a few things with our table that is holding our Angry Face objects. Since it is numerically indexed we can check it’s size with the hash/pound operator (#). And let’s check the current variable type stored in the 4 index locations of our table.

 

Add the following code –

 

Now when you run this code, in the console output area you should see the following –

  • (which is the size of the table… even though the 3rd element is now Nil)
  • table
  • table
  • nil
  • table

 

 

You should see something that looks like this on the simulator –

 

 

 

You could make another another table that is associatively indexed. Try the following for yourself –

  • Create another emtpy table
  • Call the new table “myAssoc“.
  • Allocate two more Angry Face objects to this new table.
  • Make one of the key names “AngryRed”
  • Make the other key “AngryGreen”
  • Set the color (using Corona’s method setFillColor) of “AngryRed” to red
  • Set the color (using Corona’s method setFillColor) of “AngryGreen” to green

 

When you do this, your app should now look something like this (if you are having trouble I’ve pasted the entire code snippet below) –

 

 

Here is the full code snippet –

 

As you can see… Lua tables is a big subject… and we’ve covered alot (but definitely not everything about Lua tables). You can also manipulate your tables using the built in Lua table library and iterate through your tables using Iterators. We’ll cover both of these in a future lesson.

 

See you in Lesson 6…

 

Leave a Reply