OOP in game (Classes, Objects, Inheritance, Methods, Properties)

Does anyone has experience in this matter?
I have seen OOP Game Framework on Code Exchange but it is build on Prototyping concept without Metatables (which has bad impact on performance) and with module(…, package.seeall) which is against encapsulation.

I would love to see some discussion here.

[import]uid: 22837 topic_id: 5342 reply_id: 305342[/import]

Is not using metatables bad for performance? Do you have any documentation to back that up? I had asked a while ago if the approach without metatables impacts performance but there was no response.

http://developer.anscamobile.com/forum/2010/12/17/would-using-metatable-improve-code [import]uid: 12108 topic_id: 5342 reply_id: 17763[/import]

check graham’s comment here about memory usage without metatables
http://developer.anscamobile.com/forum/2011/01/19/big-refactor#comment-17393
[import]uid: 6645 topic_id: 5342 reply_id: 17765[/import]

@jhocking http://lua-users.org/wiki/SimpleLuaClasses at Implementation of class() section.

@jmp909 thank you for the link. Now i am using method described on http://lua-users.org/wiki/ClassesAndMethodsExample with extension for inheritance and my own getters/setters. It would be great to met people who are using OOP in Corona and share experience. Do You use it for Your Corona projects?

I will try to contact Graham on his OOP implementation and present here results on my Corona OOP research.

[import]uid: 22837 topic_id: 5342 reply_id: 17770[/import]

Interesting. Now I may be mis-reading it, but your link piotr seems to indicate that the implementation without metatables is faster:
But this method will give better performance, at a cost of making the class objects somewhat fatter.

However the tradeoff as noted in jmp’s link is greater memory usage. These factors make sense (after all, using metatable would mean multiple lookups in multiple tables) so I guess I should test to see if they’re true. [import]uid: 12108 topic_id: 5342 reply_id: 17782[/import]

@jhocking did You read from the beggining of this article? The bad performance refer to section “A simplified way of creating classes” which describes that “one supplies an initialization function to the new class, and a ‘constructor’ is automatically generated.”

In the metatables implementation the constructor is explicit:

function Account.create(balance) local acnt = {} -- our new object setmetatable(acnt,Account) -- make Account handle lookup acnt.balance = balance -- initialize our object return acnt end [import]uid: 22837 topic_id: 5342 reply_id: 17789[/import]

did You read from the beggining of this article?

I’ve only had time to skim it, which is why I said I may be mis-reading. Thanks for quoting the exact line. However, I still don’t see what that line is saying about performance. It describes how the implementation works, but doesn’t say anything about performance.

The only reference to performance I see on that page is the line I quoted. Here it is again with preceding lines for more context:

It handles inheritance by copying the fields of the base class into the derived class. This isn’t the only way of doing inheritance; we could make __index a function which explicitly tries to look a function up in the base class(es). But this method will give better performance, at a cost of making the class objects somewhat fatter.

Although it is talking about fields and not methods, the same logic should apply (or rather I think it should also apply; please refute if I am incorrect.) This quote specifically states that copying data from the base class gives better performance but at the cost of greater memory usage.

Honestly though this kind of feels like a moot point to me. I doubt either CPU speed or code memory would be the bottle-neck for an iPhone game/Corona app; texture memory would run out first, so this entire discussion feels a bit like premature optimization to me. [import]uid: 12108 topic_id: 5342 reply_id: 17813[/import]

@jhocking hmm… maybe i get it wrong and you have right. Thats why i have created this thread. To share our experience and discuss. I would like to write clean maintainable code with classes. I can implement packages thx to CPM (folders) and i would like to find best way to implement classes and handle unloading the objects.

Do you have any experience with that? I think all classes should have its own destroy() method to handle freeing memory. [import]uid: 22837 topic_id: 5342 reply_id: 17838[/import]

Well as I said there was no response when I asked about metatable performance before, so hopefully some other people chime in now.

Do note though that I’m fairly loose and easy about how I think about object oriented programming, so that bias is going to show in the methods I use. I don’t really like to be strict about encapsulation or even typing, in favor of programming styles that give you a lot of flexibility and expressive power. For example, I’ve always had this notion in the back of my head that Java was designed with straight-jackets to limit the amount of damage done by bad programmers.

Oh and sure I write remove() methods for all my objects. They aren’t called automatically though; I have to call the remove() method before nil-ing out objects. *shrug* [import]uid: 12108 topic_id: 5342 reply_id: 17888[/import]

@jmp909: Sorry I didn’t respond before about that link, thanks for pointing it out. Note though that Graham said “I’m not entirely sure if this will actually help.” It doesn’t answer the question, it just, well, asks the same question. [import]uid: 12108 topic_id: 5342 reply_id: 28924[/import]