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

posted in: Tutorial | 0

Welcome to Lesson 4! In this lesson we’ll discuss how to use Lua Functions!

When you start to use Lua Functions you will begin to Reduce, Re-use, Recycle your code! (Yeap the 3 R’s… like the environmental slogan you might have come across before)

 


Overview Map: Lesson 4


 

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

 

Learn Lua and Corona Lesson 4 - Overview 1000px

 

In the above map you can see that we’ll be learning about Lua Functions and I’ll also touch briefly on Scopeand again, you will also notice that we’ll be working within the file called “Main.lua”.

 




 

Create a New Project –

Similar to Lesson 3,  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 4”
  • 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 3, 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 3…. 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 –

 

What is a Function?

 

A function is the basic way for you to create a re-usable block/chunk of code in your programs. The function does something specific and every time you call on that function it will re-do what it is suppose to do. Using functions will save you from having to write unnecessary code… over and over.

 

You can create functions that –

  • Execute some particular code chunk/block only
  • Receive a fixed number of input parameters, then execute the code chunk/block
  • Receive a fixed number of input parameters, then execute the code chunk/block, then return outputs
  • Receive a variable number of input parameters, then execute the code chunk/block, then return outputs

 

Visually I think of these 4 scenarios like shown in the following 4 figures –

 

Execute some particular code chunk or block only

 

Receive a fixed number of input parameters, then execute the code chunk/block

Receive a fixed number of input parameters, then execute the code chunk/block, then return outputs

 

Receive a variable number of input parameters, then execute the code chunk/block, then return outputs

 

Defining a Function

 

To define a Lua function, the standard way is to simply use the keyword “function“, followed by the name of that function, then define the block of code that does something, then the keyword “end“, here is a simple example –

 

 

In the above example the function “sayHello” would simply print a message to the console. However, it doesn’t actually print the message yet…. because we have not called the function yet.

 

To call the function add the following line of code –

When you add the open and close braces after the function name (just below the function definition), this will actually tell Lua to “run the block of code defined within the sayHello function“.

 

Note: In most cases we don’t want to define the function to the Lua Global Environment as outlined in Lesson 2, so from now on we’ll define our lua functions to be Local functions… which are only available to the file they are defined in (in our case, only available within “main.lua” which is perfectly fine in this case).

 

So you should re-write the code as –

 

When you run the line of code above, there is no appreciable difference in the output to the Console. It again… simply prints “Hello to You!”.

 

Functions with 1 Input

Like shown in the Figure above, functions can also be defined to accept inputs.

From within the function definition (i.e. the code that tells the function what it is to do) we call these inputs “parameters“.

The stuff (the inputs) that you send the function when you make a call to the function, we call “arguments“.

 

Let’s look at a simple example like the one above but our function will now accept a single input, then execute the code within itself –

 

Now when you run this block of code above you should see the following in the console –

“Hello to Tom !”

 

Go ahead and call this new “sayHelloTo” function a couple more times… but put in the names of your friends –

 

In this case above our Function expects 1 input… and it should be of type string, although in this case you could send it a number and it won’t fail. However, if you don’t send it an input string… it will fail because the input parameter within the function “name” isn’t defined and defaults to Nil… and when trying to concatenate in the print statement it fails since you can’t combine type string with type Nil.

 

Functions with Multiple Fixed Inputs + 1 Output

Next, let’s make a function that takes in two input numbers….. does something with them (like adds them together), then returns a single output (the result) –

 

 

In the block of code above we perform a simple sum and return the result. We then print the result, defined as variable “Ans” to the console.

 

Functions with a Variable number of Inputs

But what if you don’t know in advance how many inputs will be passed to your function? Or if you want to call it with 1 input and then later call it with 3 inputs?

We’ll… in Lua there is a special case where you can define the function to handle a variable number of inputs that I call the “3 dots” –

 

 

To access these inputs within the function you use the keyword “arg” along with the particular index for each input parameter.

Let’s look at an example that takes a variable bunch of numbers and simply returns back to us how many arguments we sent the function when we called it –

 

 

When the above code block is called you should see in the Console the message –

“The number of inputs passed in was: 11”

 

If you wanted to compute the actual Sum of all these numbers you could do the following (bearing in mind we haven’t yet covered For loops…. that’ll be in a later lesson)

 

 

When the above code block is called you should see in the Console the message –

“The number of inputs passed in was: 11”

“The ans to your Sum = 382”

 

Another way to Define a Function – “Anonymous”

Okay… I admit… when I first came across the use of “anonymous” functions several years ago it was bizarre! When I learned “ANSI C” way back in 1999, the standard way to define a function in C was not too different to the standard way to define a function in Lua.

When I first saw a function on the right-hand-side of the “equal sign”… with no function name next to the “function” keyword…  I was like “what is that???!??”. Here is an example –

 

 

Now, when you run this code… if executes perfectly fine and you should see in the Console –

 

“Gday, Paul !”

 

Since within Lua a function is an actual “type”… you can assign a “nameless” function to a variable. The variable is then an alias for that “nameless” function definition. These are generally referred to as Anonymous Functions. They are perfectly legal in Lua and many other languages.

If you’ve ever come across some Javascript code…. you’ve no doubt encountered Anonymous Functions… and something else called “Closures”.

At this point know they exist…. and we’ll generally use the standard / normal way to define functions in these lesson… but sometimes we will define anonymous functions where they have an advantage.

 

Using Functions with the Corona Display API

 

Now, let’s get back to the simple App we started to define in Lesson 3 –

We’ll start by defining some constant Global variables. Then we’ll define a function that will create an AngryFace image on the screen each time it is called. We’ll call this function “makeAngryFace” and we’ll define it to have 4 input parameters (x position, y position, scale, rotation) 

 

After our function definition, we start off by creating our “purple” background by using the “newRect” method in Corona’s Display API.

We then add the title “Angry Faces App” using our custom font from Lesson 3… using the “newText” method in Corona’s Display API.

We then make a call to put our Angry Face image on the screen. We pass it 4 arguments –

  • The x position is defined at the center of the screen (using our Global variable) “_CX
  • The x position is defined at the center of the screen (using our Global variable) “_CY
  • We scale then image by 50% (passing in 0.5)
  • We rotate the image by 30 degrees (passing in 30)

 

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

 

Next we see the real benefit of our function. If we want to create a few more instances of the Angry Face image on the screen, rather than having these following lines over code over and code…

 

We simply make a few additional calls to our “makeAngryFace” function… and pass in the 4 inputs required as specified in the function definition –

 

 

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

 

 

 

 

See you in Lesson 5…

 

Leave a Reply