New to corona and lua, a little help on function definition with lua object method

So I’m just starting the corona getting started guide and in chapter 4 which is creating scenes. So at one point the function used to create scene is defined as function scene:create(event ) instead of using local function (…) like it’s generally done to create a new function, the description says this is done to associate this function with the scene variable, however I don’t understand how this is done. From what I can see the “:” is used whenever you pass the function variable in as well to use things like self etc but I dont understand how it’s used here with function scene:create ( event ), what does it mean by associating this function with the scene variable, does it create a new table in scene where this function is stored, if so in what way? 

Thanks for the help! 

1 Like

This is a great question.

Corona has several ways to define functions.  When you do:

local function myFunction()

You are creating a standalone function that can be called from the current module as long as you define it before you use it.  For many functions that are local to that scene, this is the right way to define them.

However, Lua also supports “table functions”. These create an “Object Oriented” like interface for doing things.   Consider you have an object named “player” and an object named “enemy”. Both need a “move” function.  With Lua, you can create your player object:

local player = {}

and of course put in properties like numberOfLives, health, etc.

player.numberOfLives = 3 player.health = 10

You can also add functions to the table:

function player:fire( yourParameters ) function enemy:fire( yourParameters )

This way the object owns the function instead of the module owning it.  There are two syntaxes you will run into:

function player.fire(self, yourParameters) function player:fire( yourParameters )

When you use a colon instead of a dot, you implicitly pass the instance of the object to the function. This is done assuming you will have multiple copies of the same object. You might only have one player, but you certainly might have multiple enemies and you want your function to work on the right one, so using the colon operator lets you automatically pass “self” to the function.

Corona’s Composer uses table functions because you could technically have multiple scenes in one Lua file (not recommended). This is why you see mixed ways of doing functions.  The ones with “local” are not tied to specific objects and just do the work they are asked. The ones that are:  function object:functionName() assume you want to work with that specific object.

Rob

1 Like

This is a great question.

Corona has several ways to define functions.  When you do:

local function myFunction()

You are creating a standalone function that can be called from the current module as long as you define it before you use it.  For many functions that are local to that scene, this is the right way to define them.

However, Lua also supports “table functions”. These create an “Object Oriented” like interface for doing things.   Consider you have an object named “player” and an object named “enemy”. Both need a “move” function.  With Lua, you can create your player object:

local player = {}

and of course put in properties like numberOfLives, health, etc.

player.numberOfLives = 3 player.health = 10

You can also add functions to the table:

function player:fire( yourParameters ) function enemy:fire( yourParameters )

This way the object owns the function instead of the module owning it.  There are two syntaxes you will run into:

function player.fire(self, yourParameters) function player:fire( yourParameters )

When you use a colon instead of a dot, you implicitly pass the instance of the object to the function. This is done assuming you will have multiple copies of the same object. You might only have one player, but you certainly might have multiple enemies and you want your function to work on the right one, so using the colon operator lets you automatically pass “self” to the function.

Corona’s Composer uses table functions because you could technically have multiple scenes in one Lua file (not recommended). This is why you see mixed ways of doing functions.  The ones with “local” are not tied to specific objects and just do the work they are asked. The ones that are:  function object:functionName() assume you want to work with that specific object.

Rob