Question abour defining functions in LUA.

Hi,

I was diving into Corona documents reading bits about this and that here and there that I stumbled upon this:

[lua] local t = {}
function t:timer( event )
local count = event.count
print( “Table listener called " … count … " time(s)” )
if count >= 3 then
timer.cancel( event.source ) – after 3rd invocation, cancel timer
end
end

– Register to call t’s timer method an infinite number of times
timer.performWithDelay( 1000, t, 0 )[/lua]

What caught my eye is the second line, the definition line: function t:timer( event )

It may be simple but since I’m new to LUA (and dynamic languages, beside my old experiences with AS2, 3) I could understand what does that double colon mean. My best guess was that it added this timer to that “t” table but since there was no add statement, I highly doubt it.

I Googled around and read more than 20 tutorials on function definition in LUA but none of them had this, so it has to be with something else, like that table “t” for example.

Thanks and sorry if this is a simple question, but we are in the n00b section! :] [import]uid: 206803 topic_id: 34764 reply_id: 334764[/import]

I think the first couple paragraphs the lua documentation should sum it up quite well:
http://www.lua.org/pil/16.html#ObjectSec

“A table in Lua is an object in more than one sense. Like objects, tables have a state. Like objects, tables have an identity (a selfness) that is independent of their values; specifically, two objects (tables) with the same value are different objects, whereas an object can have different values at different times, but it is always the same object. Like objects, tables have a life cycle that is independent of who created them or where they were created.”

OOP is a paradigm that makes my head a little fuzzy :frowning: [import]uid: 62706 topic_id: 34764 reply_id: 138129[/import]

Thanks for the reply, I have no problem with OOP or OOD. Challenge is probably at definition of tables. I know functions can be data types in LUA, I just can’t correlate that definition with that table. [import]uid: 206803 topic_id: 34764 reply_id: 138140[/import]

@CraftyDeano has it right, but in layman’s terms,

library.doThis() is a function in a table.

library:doThis() is basically the same as saying library.doThis(self), where self is the table (“library”). this allows you to make easy function setups.

For example, in the OP example, it’s the same as saying t.timer(self, event). This is super useful because you can write functions in seperate, clean pieces.

[code]local function swim(self)
print(self.name…" is swimming!")
end

local function makeFish()
local object = display.newRect(0,0,32,32)
object.name = “Bill”
object.swim = swim
return object
end[/code]

In this code, you can put swim by itself. Even in another file if you wanted. But your core function doesn’t have to nest functions-inside-functions. You can just make the shortcut and use “self” to bridge the gap. [import]uid: 41884 topic_id: 34764 reply_id: 138146[/import]

I just started learning Corona last week. I come from an AS2-3 background, so I kind of see it like this:

In AS:

object = new Object();

In LUA:

object = {} -- known as a table

When adding method to an object in AS:

object.getValue = function () { return this.value; }
Adding method in LUA:

object:getValue = function () return self.value end

From what I understand so far is using:

table:someMethod()

Infers that the “self” is available in scope

Using…

table.someMethod() --notice dot not colon

…is not the same. So you can’t do something like:

object.doMethod() return self.value -- there is no "self" in this situation end

So far I use “:” for calling methods on the object(table), and stick to the “.” for properties. But LUA has a whole bunch of different ways to approach things. I’m just trying to apply my AS knowledge. I’m not even sure if this helps you with your question, but there you go. Cheers.
[import]uid: 202223 topic_id: 34764 reply_id: 138150[/import]

Thanks richard9 and develephant for your answers,

So the “timer” function is defined and attached to the “t” table, right?

I’ve read many many articles regarding this and found it most useful and I leave it here for future googlers finding this:
http://www.ludicroussoftware.com/blog/2011/08/10/object-oriented-function-calls-in-corona/

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

One more thing though, in the performWithDelay line:

timer.performWithDelay( 1000, t, 0 )

He passed the table, “t”, as parameter to performWithDelay, which I believe will be called infinitely with 1000ms periods. Question is, why he passed a table instead of a function? Isn’t it most logical if you pass a function to performWithDelay and expect it to run it as intervals that you define instead of a table? What if that table object, here “t”, has numerous functions attached to it, would performWithDelay call all of them or there is a secrecy here that I’m not ware of?

Thank you all Coronaville residents. [import]uid: 206803 topic_id: 34764 reply_id: 138172[/import]

I found your observation interesting and it looks like the answer can be found here:

http://developer.coronalabs.com/content/events-and-listeners#Touch_Events (Search the page for “Timer events”)

Basically, there are a set of default listener names for different events like “enterFrame”, There is also an event called “timer”. In the code the author is setting up the default “timer” event listener specifically on the table. When “t” is passed as the function for the timer, the t:timer is being automatically called, because it is the default.

Using the enterFrame event as a more recognizable example;

t = {}  
function t:enterFrame( event )  
 --doing enterFramey stuff here  
end  
  
Runtime:addEventListener( "enterFrame", t ) -- t:enterFrame will be called, it's the "enterFrame" default  

I’m pretty sure the authors code is set up for better memory handling, using the local table, etc. because I think you could also do something like:

local timerListener = function ( event )  
 --timer updating  
end  
timer.performWithDelay( 1000, timerListener, 0 )  

I’m not sure about that 100%, still learning myself. And I just learned some new stuff, so thanks! Cheers.
[import]uid: 202223 topic_id: 34764 reply_id: 138232[/import]

I think the first couple paragraphs the lua documentation should sum it up quite well:
http://www.lua.org/pil/16.html#ObjectSec

“A table in Lua is an object in more than one sense. Like objects, tables have a state. Like objects, tables have an identity (a selfness) that is independent of their values; specifically, two objects (tables) with the same value are different objects, whereas an object can have different values at different times, but it is always the same object. Like objects, tables have a life cycle that is independent of who created them or where they were created.”

OOP is a paradigm that makes my head a little fuzzy :frowning: [import]uid: 62706 topic_id: 34764 reply_id: 138129[/import]

Thanks for the reply, I have no problem with OOP or OOD. Challenge is probably at definition of tables. I know functions can be data types in LUA, I just can’t correlate that definition with that table. [import]uid: 206803 topic_id: 34764 reply_id: 138140[/import]

@CraftyDeano has it right, but in layman’s terms,

library.doThis() is a function in a table.

library:doThis() is basically the same as saying library.doThis(self), where self is the table (“library”). this allows you to make easy function setups.

For example, in the OP example, it’s the same as saying t.timer(self, event). This is super useful because you can write functions in seperate, clean pieces.

[code]local function swim(self)
print(self.name…" is swimming!")
end

local function makeFish()
local object = display.newRect(0,0,32,32)
object.name = “Bill”
object.swim = swim
return object
end[/code]

In this code, you can put swim by itself. Even in another file if you wanted. But your core function doesn’t have to nest functions-inside-functions. You can just make the shortcut and use “self” to bridge the gap. [import]uid: 41884 topic_id: 34764 reply_id: 138146[/import]

I just started learning Corona last week. I come from an AS2-3 background, so I kind of see it like this:

In AS:

object = new Object();

In LUA:

object = {} -- known as a table

When adding method to an object in AS:

object.getValue = function () { return this.value; }
Adding method in LUA:

object:getValue = function () return self.value end

From what I understand so far is using:

table:someMethod()

Infers that the “self” is available in scope

Using…

table.someMethod() --notice dot not colon

…is not the same. So you can’t do something like:

object.doMethod() return self.value -- there is no "self" in this situation end

So far I use “:” for calling methods on the object(table), and stick to the “.” for properties. But LUA has a whole bunch of different ways to approach things. I’m just trying to apply my AS knowledge. I’m not even sure if this helps you with your question, but there you go. Cheers.
[import]uid: 202223 topic_id: 34764 reply_id: 138150[/import]

Thanks richard9 and develephant for your answers,

So the “timer” function is defined and attached to the “t” table, right?

I’ve read many many articles regarding this and found it most useful and I leave it here for future googlers finding this:
http://www.ludicroussoftware.com/blog/2011/08/10/object-oriented-function-calls-in-corona/

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

One more thing though, in the performWithDelay line:

timer.performWithDelay( 1000, t, 0 )

He passed the table, “t”, as parameter to performWithDelay, which I believe will be called infinitely with 1000ms periods. Question is, why he passed a table instead of a function? Isn’t it most logical if you pass a function to performWithDelay and expect it to run it as intervals that you define instead of a table? What if that table object, here “t”, has numerous functions attached to it, would performWithDelay call all of them or there is a secrecy here that I’m not ware of?

Thank you all Coronaville residents. [import]uid: 206803 topic_id: 34764 reply_id: 138172[/import]

I found your observation interesting and it looks like the answer can be found here:

http://developer.coronalabs.com/content/events-and-listeners#Touch_Events (Search the page for “Timer events”)

Basically, there are a set of default listener names for different events like “enterFrame”, There is also an event called “timer”. In the code the author is setting up the default “timer” event listener specifically on the table. When “t” is passed as the function for the timer, the t:timer is being automatically called, because it is the default.

Using the enterFrame event as a more recognizable example;

t = {}  
function t:enterFrame( event )  
 --doing enterFramey stuff here  
end  
  
Runtime:addEventListener( "enterFrame", t ) -- t:enterFrame will be called, it's the "enterFrame" default  

I’m pretty sure the authors code is set up for better memory handling, using the local table, etc. because I think you could also do something like:

local timerListener = function ( event )  
 --timer updating  
end  
timer.performWithDelay( 1000, timerListener, 0 )  

I’m not sure about that 100%, still learning myself. And I just learned some new stuff, so thanks! Cheers.
[import]uid: 202223 topic_id: 34764 reply_id: 138232[/import]