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

posted in: Tutorial | 0

Welcome to Lesson 1!

 

Here we will cover the different variables and types in the Lua programming language. And we will see how to place comments in our code and how to output values and information by using the Print function.

 

Check out the video tutorial below or click here to see it on Youtube 

 


Overview Map: Lesson 1


 

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

Learn Lua and Corona Lesson 1 - Overview 1000px

 

 

In the above map you can see that we’ll be learning about Variables, Types, Comments and Print()… but you will also notice that we’ll be working within a file called “Main.lua”.






Download and Install Corona SDK –

 

If you haven’t installed Corona yet, head over to https://coronalabs.com. Click on the “Download” button in the top right hand side of the page. You will need to register, accept the terms/conditions and you’ll be able to download the SDK for either Windows or Mac. I recommend you check out “Corona SDK System Requirements” and “Installing Corona SDK” for your O/S within Corona University.

Use or choose your favourite text editor, I will be using Sublime Text 3 since Corona has their free plugin “Corona Editor” available with Sublime Text that features code completion for the API, debugging options and other handy features.

 

This is the setup I will be using throughout this tutorial series.

 

If you want a guided tour of the installation process, check out this video –

 






 

Create a New Project –

To be able to explorer the Lua programming language we will first create a new Project within the 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 1”
  • 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”

 

Learn Lua and Corona Lesson 1 - Workflow2

 

Hit the OK button in the New Project window above and the window for your new project should launch (otherwise just navigate to the Project Folder location, like that shown in the New Project window above) –

 

Learn Lua and Corona Lesson 1 - Workflow4

 

Initially, I don’t want you to worry about any other file except “main.lua“. This is the file where we’ll type all our code for these initial lessons.

 

We’ll cover the importance of the other files and default PNG images that Corona creates in future lessons.

 

You will notice that a blank Corona Simulator window opens along with the Corona Simulator Output window (console). It should look something like this –

 

Learn Lua and Corona Lesson 1 - Workflow3

 

Open up “Main.lua” –

 

Main.lua is the very first file that loads when our game/app launches on a persons device. If you think of your app as a Stage Play, then Main.lua is like having everything setup and ready on the stage… before the curtains open to reveal the actual play.

It is always the first file that gets loaded with any Corona SDK app or game. Technically you could create your entire game or app by just adding code to Main.lua only. However, that might get pretty messy and complicated depending on the size of your game or app…. so in future lesson’s I’ll show your how to use the official Corona SDK scene management library called Composer.

 

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.

 

Learn Lua and Corona Lesson 1 - Workflow5

 

Then, in the Sidebar within Sublime Text 3, click on the file at the bottom “main.lua”.

 

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 as shown in the screen shot below.

 

Learn Lua and Corona Lesson 1 - Workflow6

 

 

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

 

Lua Version

Unless otherwise specified, the Lua language descriptions in this tutorial series cover Lua language version 5.1.

At the time of writing the most recent version of the Lua interpreter is version 5.3. However, Corona SDK and I think most other game development framework are packaged with Lua 5.1… so that is the Lua documentation version we will reference. The fundamentals from version 5.1 and above are basically the same… so if you were using a later Lua interpreter for some reason your written code should be perfectly fine.

 

When we get into the code below, feel free to verify what Lua version you have pre-installed with Corona SDK by adding the following line anywhere –

 

 

The version should be “Lua 5.1” and will show up in the output console when adding that line of code.

 

Comments and Print() in Lua

 

Although somewhat un-exciting…. two import features of Lua are comments and the built-in Print() function.

If you plan to maintain and read your code.. days, weeks, months and years after your have written the code then..


Make sure you always properly comment your code!


 

Comments:

 

In Lua, your can produce a single line comment by using the “double dash” or “double hyphen”. It looks like this –

In fact, when you created the new Corona SDK project above you can see that the Main.lua file was automatically created with some single line comments in it.

 

Next, the multi-line comment. Here you use the “double dash / double hyphen”, followed by two left “square brackets”, and ending with two right “square brackets”. The multi-line comment looks like this –

 

The Print() Function:

 

I use the built-in print() function all the time when making games and apps. Not only is it great for debugging… but also great for understanding what current block of code has executed when testing your app in the Corona SDK.

This will make more sense as we use it in the code examples below…


Remember… Print() is your first debugging tool! 


The print() function is used to output any information that you want to display in the output console. We’ll see it in action below so I won’t show any code examples quite yet.

 

Variables and Types in Lua

 

There are 8 basic types in Lua which are:

  • Nil
  • Number
  • Boolean
  • String
  • Table
  • Function
  • Userdata
  • Thread

 

Here we will only cover the first 4 mentioned above and we’ll cover the more advanced types like Tables and Functions in their own tutorials later.

 

 

Lua Type: Nil

 

The default type in Lua is Nil. If you create a variable in Lua and do not initially assign it a value.. then it’s type will be defined as Nil.

Let’s see this in action!

In the file Main.lua, type in the following –

 

 

In the code above we created a variable called myVar1 but did not assign it any particular value.

We also created another variable called myVar2 and specifically assigned it Nil.

Note: When I say “we assigned it the value”… this means we use the “equal sign” =.

And the “value” is the part that is on the right side of the “equal sign”.

 

Let’s verify that both these variables are actually defined as Nil. To run the code above and view the output we used the Lua built-in functions: print() and type().

 

Before we run this project from within Sublime Text, let’s make sure that we can see the built-in output console window.

 

Go to the Corona Editor tab. Ensure you select Show Build Panel. You should see a bottom panel appear within Sublime Text.

 

Learn Lua and Corona Lesson 1 - Workflow7

 

Next, launch the Corona Simulator from within Sublime Text by hitting Windows Key + F10 (On Windows), CMD + F10 (On Mac), or just navigate to the Corona Editor plugin menu as shown below –

 

Learn Lua and Corona Lesson 1 - Workflow9

 

 

You should then see the following output in the Console Output window –

 

Learn Lua and Corona Lesson 1 - Workflow8

 

Since Lua has automatic memory management, there is something called a garbage collector. When you are done with a variable in your program you can assign it Nil and the garbage collector will clean up the computer memory that your variable occupied.

 

Lua Type: Number

 

Whether you want to define an integer or some decimal numbers… any numerical value in Lua is defined as the type: Number. All numbers are stored internally within Lua as double-precision floating point numbers.

Let’s see this in action!

Continue in the file Main.lua, and add in the following extra lines of code –

 

Re-launch the Corona Simulator from within Sublime Text by hitting Windows Key + F10 (On Windows), CMD + F10 (On Mac).

You should then see the following output in the Console Output window –

 

Learn Lua and Corona Lesson 1 - Workflow10

 

Lua Type: Boolean

 

The boolean type can only have two distinct values: true or false.

It should be specifically noted that in Lua, conditional statements such as “if else” (which we’ll get into later) consider both the the boolean value false and type Nil to be false. Everything else that evaluates in a conditional statement is assumed to be true.

Note: Be careful!…. Because Lua considers both the numeric value 0 and an empty string (which we’ll cover next) to evaluate as true in conditional tests. This is different than some other programming languages.

Okay, let’s see Boolean’s in action!

Continue in the file Main.lua, and add in the following extra lines of code –

 

Re-launch the Corona Simulator from within Sublime Text by hitting Windows Key + F10 (On Windows), CMD + F10 (On Mac).

You should then see the following output in the Console Output window –

 

Learn Lua and Corona Lesson 1 - Workflow11

 

Although we did not get into the condition IF ELSE statement yet we used them above to prove a point. Try changing the boolean variable ‘isCorrect’ to false… and then reassign its type to nil, 0, and the empty string ” “.

If the IF ELSE statement evaluates to true, then you will see the text “In IF part” printed to the console. Otherwise the IF ELSE statement evaluates to false, and you will see the text “In ELSE part” printed to the console.

 

 

Lua Type: String

 

In Lua the term string simple means a sequence of characters.

You can define a variable as the type string by surrounding your sequence of characters in single quotes ‘ ‘ …. or double quotes ” “.

 

Similarly you can assign a multi-line string with double left square brackets [[, followed by double right square brackets ]].

 

Certain escape sequences as supported with strings such as the newline “\n”, horizontal tab “\t” and many others.

 

To do string concatenation in Lua you have to use the double dot operator .. between strings. We’ll do this in the code below.

 

Let’s see what we can do with strings, go ahead and add the following lines of code to Main.lua

If you try to concatenate a string with a number, an implicit call of the tostring() function is made on the number variable.

Conversely, if you were to try to concatenate a string with a boolean, you need to make an explicit call to the tostring() function so that you won’t get an error.

 

A very convenient way to find the length of a string is to use the ‘hash’ or ‘pound’ operator.

 

Re-launch the Corona Simulator from within Sublime Text by hitting Windows Key + F10 (On Windows), CMD + F10 (On Mac).

You should then see the following output in the Console Output window –

 

Learn Lua and Corona Lesson 1 - Workflow12

 

See you in Lesson 2…

 


Video Tutorial Here –

 

 


Leave a Reply