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.
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 –
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”
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) –
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 –
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.
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.
What you should see within Main.lua is the following commented code –
1 2 3 4 5 6 7 |
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here |
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 –
1 2 |
-- Let's check what Lua interpreter version we are using print(_VERSION) |
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 –
1 |
-- This is a single line comment (The Lua Interpreter will ignore this line) |
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 –
1 2 3 4 5 |
--[[ This is a multi-line, until the Lua Interpreter sees the final right square brackets, everything in this text sequence will be ignored ]] |
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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local myVar1 local myVar2 = nil print(type(myVar1)) print(type(myVar2)) |
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.
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 –
You should then see the following output in the Console Output window –
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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here -- Let's explore the type 'nil' local myVar1 local myVar2 = nil print(type(myVar1)) print(type(myVar2)) -- Let's explore the type 'number' local myNum1 = 5 local myNum2 = 6.0 local myNum3 = myNum1 + myNum2 print(myNum3) print(type(myNum3)) |
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 –
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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here -- Let's explore the type 'nil' local myVar1 local myVar2 = nil print(type(myVar1)) print(type(myVar2)) -- Let's explore the type 'number' local myNum1 = 5 local myNum2 = 6.0 local myNum3 = myNum1 + myNum2 print(myNum3) print(type(myNum3)) -- Let's explore the type 'boolean' local isCorrect = true -- try changing the value from true to false, nil, 0 and empty string " " if isCorrect then print("In IF part") else print("In ELSE part") end |
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 –
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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here -- Let's explore the type 'nil' local myVar1 local myVar2 = nil print(type(myVar1)) print(type(myVar2)) -- Let's explore the type 'number' local myNum1 = 5 local myNum2 = 6.0 local myNum3 = myNum1 + myNum2 print(myNum3) print(type(myNum3)) -- Let's explore the type 'boolean' local isCorrect = true -- try changing the value from true to false, nil, 0 and empty string " " if isCorrect then print("In IF part") else print("In ELSE part") end -- Let's explore the type 'string' local myString1 = "hello world" -- string using double quotes local myString2 = 'hello you' -- string using single quotes -- try some string concatenation local myString3 = myString1 .. " and " .. myString2 print(myString3) local bigString = [[this is a very long string that spans many lines so you use the double square brackets and you don't need single or double quotes]] print(bigString) local mashTogether = myString1 .. myNum1 print(mashTogether) -- notice above in variable 'mashTogether' an implicit tostring() call was made on variable 'myNum1' local someString = "Is that really " .. tostring(isCorrect) print(someString) -- find the length of a string using the Hash or Pound operator local sizeOfString = #bigString print(sizeOfString) |
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 –
See you in Lesson 2…
Video Tutorial Here –
Leave a Reply
You must be logged in to post a comment.