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

posted in: Tutorial | 0

Welcome to Lesson 2!

 

In this lesson we’ll discuss the differences between Local and Global variables.

In the last lesson we were exclusively using Local variables, as these are the types of variables you should almost always be using when creating your mobile games and apps.

You should use Global variables infrequently… and I’ll show you cases where I do use them.


Overview Map: Lesson 2


 

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

 

Learn Lua and Corona Lesson 2 - Overview 1000px

 

In the above map you can see that we’ll be learning about Local and Global variables… but you will also notice that we’ll be working within the file called “Main.lua”.




 

Create a New Project –

Similar to Lesson 1,  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 2”
  • 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 1, 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 in this lesson.

 

Open up “Main.lua” –

 

 

Similar to Lesson 1…. 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 –

 

Global and Local Variables

 

 

As you saw in Lesson 1, when we created our variables we always used the keyword ‘local’ to the left of the variable name, such as –

This is good practice as a local variable will give you a performance improvement over a global variable.

 

Unlike global variables, local variables are visible only in the block or chunk of code where they are declared.

 

If a local variable is declared at the top of a particular file such as “scene1.lua” shown in the overview map above… it will be available all throughout that file. If a local variable is declared within a particular chunk of code inside “scene1.lua”, it will only be available within that chunk of code.

 

In contrast, Global variables live as long as your application is running. If you define a Global variable, it can be seen throughout your entire application (regardless of what Lua file is currently loaded by the Lua Interpreter).

 

You can delete a global variable by assigning nil to it and it will be garbage collected.

 

To define a Global variable you simply assign a value to a variable name (without any keyword ahead of it).

 

Lua’s Global Table “The Environment”

 

As outlined in “Programming in Lua” by chief Lua architect Roberto, Lua keeps all its global variables in a regular table, called the environment. As mentioned in the previous lesson, tables are a type in Lua (the most important type!)

The “environment table” also known as Lua’s global’s table is defined by the single name “_G”.

 

So every time you define a datatype without the local keyword in front of it, Lua will add the variable name from that datatype to it’s “_G” table.

 

If you are going to define Global variables then it is good practice to –

  • Explicitly allocated it to the “_G” table by using “_G.myVar” type of notation
  • Only define your Global variables near the top of the “Main.lua” file or
  • Create a separate module/file called “myGlobals.lua” and then simply require that module/file into “Main.lua”

 

If we expand on the code above we’ll see the following –

 

Now compared to the number of Globals (counter variable “i”) defined previously (which are the default ones included in Lua and Corona), we should have one extra Global variable “myVar2”.

 

Next, let’s explicitly add another Global variable, by prefixing out Global declaration with the “_G.” notation such as the following –

 

 

We should have one extra Global variable “myVar3” within Lua’s Globals table.

 

Since the Global (Environment) table is simply a regular Lua Table… we can read the value assigned to the Global variable “myVar3” by doing the following –

 

 

You’ve been super patient so far in learning about these fundamentals of the Lua programming language. Next, we’ll show you how we can use the Corona display* API to get your graphics on the screen quickly… so you’ll see things start to come to life in the Corona simulator screen (that until now was simply a blank screen, since we’ve just been looking at the output in the Console / Terminal window).

 

So remember, use Local variables almost always and Global variables very sparingly….

 

See you in Lesson 3…

 

Leave a Reply