Function listeners vs Table listeners

Although I can understand why there are listeners and what they do, I can’t understand the difference between table listeners and function listeners. I mean when would one type be used instead of the other? [import]uid: 295 topic_id: 1397 reply_id: 301397[/import]

A function listener calls a function and gets the function as it’s parameter.

A table listener calls a method from the table with special name and gets the table as parameter.

Table listeners are used when working with objects. Your object is the table (that’s just how it is in lua)

If you are experienced in oop: the table listener gets the object as parameter and calls a defined method of it using the object as self.

It is well documented with sample code here:

http://developer.anscamobile.com/content/events-and-listeners

You may also look up my latest sample code for managing songs in a playlist which uses multiple tablel listeners and implement custom ones too. [import]uid: 6928 topic_id: 1397 reply_id: 3887[/import]

Thanks,

I searched the forum and googled " managing songs in a playlist which uses multiple tablel listeners" but nothing come up. Is there any other way to look it up? A keyword. Even better, a link would be highly appreciated. [import]uid: 295 topic_id: 1397 reply_id: 3888[/import]

Coud this be it?

http://developer.anscamobile.com/forum/2010/07/10/playlist-playing-multiple-songs-version-which-does-not-use-buggy-completion [import]uid: 295 topic_id: 1397 reply_id: 3889[/import]

I read http://developer.anscamobile.com/content/events-and-listeners and it doesn’t make it clear to me. It just explains what they are but not when one is best used over the other. [import]uid: 295 topic_id: 1397 reply_id: 3890[/import]

Yes that is the code I mentioned.

Your question is a bit like asking if red or blue is the best overall color. You use red if you need red… and blue if you need blue. And if you are free to decide what color to use… there is no best but the one you like the most.

Table listener are used the context of object oriented programming. If your objects need to act on events you need a table listener … a soon as the object needs information about itself.

I do not use any function listener in my code, cause everything I code is object oriented and tries to “modularize” its behavior. I try keeping external references and “knowledge” to a minimum.

There may be some code example needed… I may write something up later for you. But I would highly suggest to read about Object Oriented Programming in Lua and recommend the “Programing in Lua” Book which explains this and a lot more in detail!

Although I prefer the 2nd edition … which you need to pay for… there is the 1st edition free on the net

http://www.lua.org/pil/
[import]uid: 6928 topic_id: 1397 reply_id: 3893[/import]

Thanks.

I have both blue PIL 2nd Edition and latest Lua Reference Manual 5.1.

I read the PIL cover to cover and read the OOP section more than once but it hasn’t sunk in yet.I guess I have to read it a few more times. [import]uid: 295 topic_id: 1397 reply_id: 3898[/import]

I think it is pretty hard for people which “understood” it to explain it to people which don’t …

The approach to Object Orientation in programming at first is “complicating easy stuff” …

I believe the easiest to understand concept is like the following:

Non Object Oriented code has functions to work with objects.

In Object Oriented code the objects offer methods themself.

Take this non OO example:

txt="Hello World"  
print(txt)  

and compare with this artificial OO example:

obj=newTextObject("Hello World")  
obj:print()  

In the first code you have a variable (txt) which gets print by a function (print)

In the second code example you have an object (obj) which has a member function to print ITSELF
So you need table listeners if you work OO oriented because you want to have the object to handle events ITSELF …

From another perspective:

“print()” can only print stuff which it knows how to handle

whereas the OO approach has a way to “print” every object by asking the object to print ITSELF …

You could implement “print()” the function like this:

function print(obj)  
 obj.print()  
end  

And actually something similar is done often …
[import]uid: 6928 topic_id: 1397 reply_id: 3900[/import]