Object-Oriented Lua Tutorials

There is a common misconception about destroying objects. As I mention in my tutorial, you want to avoid the creation and destruction of objects during gameplay. Use pooling for efficient control over frame rates.

When changing scenes, Corona’s storyboard will handle all of the object destruction for you, assuming you use the methods in my guide (all custom listeners and objects should branch under the scene’s view).

The way my classes are set up, using “dispose” will pool the object (put it back on the shelf for later use). Hope that helps!

Thanks that helped. I have another question for you, why is scene on line 8 not local? I am abit confuse with the usage of it.

https://gist.github.com/ArdentKid/4958451#file-oop-scenegame-lua

On a side note, may I know what program you used to code lua? 

I use Sublime Text 3 (beta). But 2 is just as good.

I use what I’ve defined as “scene” in the global scope for custom events. This is the best way for your classes to communicate with each other, as long as you localize it at the top of each class.

I see! Now it all make sense. One last curious question from me: Would it be better to “require storyboard” in every class than to use a global variable? If not, why? 

Sorry for asking so much! Not doubting your skills but trying to understand the basics :slight_smile:

Sure, you could do that. I haven’t tried it but I’d assume it returns the right table. Either way, it shouldn’t be too big of a concern since you’ll rarely make calls to it, (i.e. speed won’t be an issue).

Hi guys, In addition to the excellent ArdentKid OOP tutorials, I am also looking to the great jetpack template from Glitch Games that you can find here:http://forums.coronalabs.com/topic/18035-free-jetpack-joyride-template/page-2?hl=jetpackI have looking at it and it seems simple enough (and the template works beautifuly!) but I do not know enough about OOP to know if there is any limitations to it and why one will use that implementation or not. It seems to be using meta tables and event listeners (dispatch and listener) like Ardentkid does. Anything I should know before using either ArdentKid or Glitch Games OOP implementation? Again, I do not know enough to really decide! Thanks for any pointers. Mo

Good find! Looks like it uses some OOP concepts well :slight_smile: But I don’t see too much complexity (no inheritance or polymorphism). Could also use a bit more organization for modularity, but that’s dependent on what you want to do with it.

There are lots of ways to do OOP in LUA, unfortunately with Corona there is no "perfect way’ due to the fact you can’t use meta tables with display objects.  So you have one OOP method for tables and one for display objects, unless you don’t use meta tables, then you can use one way for all, but there are limitations using that way.

Corona is working on a way to offer some way to do prototype style OOP, not sure how good it is or if it also has limitations, just know it is coming out.  Anyway you look at it, Lua is not OOP friendly, so you have to figure out a way that works.  Not everyone needs all features of OOP and can sacrifice things like static members or inheritance to use one way over another.

Thanks guys. I was just curious about that implementation. Good find about inheritance! Did not think about that. I guess for a newbie like me it maybe ok? I kind of like that each object (coins, jetpack…) is in seperate file (the minimum for OOP) and they have all the same structure. Your implementation is definitely more powerful. So I think you are right, it depends on what people are looking for. For me is just a way to get away from procedural programming where the code become so difficult to maintain after not working with it for few months (1000’s lines of code) I may start “slow” with this OOP implementation then later switch to yours. Since I bought your template code, I am covered for the future. What do you think of this plan? @cspence: that will web wonderful if Corona came up with it’s own OOP implementation! I have been looking at an iPad app called Codea ( lua based SDK) and even that app has some OOP features. Can’t wait… Thank you both again:) Mo

I’ve seen Codea, I have no interest in coding on an iPad.  That would be extremely painful.

I’m not sure how well the implementation would work (Corona OOP lib) but I have been talking with a few people and I really think it is something that would be super helpful.

Actually you don’t have to anymore since now they have “Air Coding” where you code on your PC and the code is transmitted to your iPad in real time through wifi. Very cool! But the point was simply that even Codea has some simple OOP which make programming easier. Indeed, it will be wonderful to have a Corona OOP! Thanks again guys for all the help. Mo

[quote name=“ArdentKid” post=“194412” timestamp=“1374536372”]Good find! Looks like it uses some OOP concepts well :slight_smile: But I don’t see too much complexity (no inheritance or polymorphism). Could also use a bit more organization for modularity, but that’s dependent on what you want to do with it.[/quote] Do you mean that the way the jetpack template implement OOP will not allow inheritance? Is inheritance super important in small/medium size game design? I will think that encapsulation is the first critical thing and it seems that example acheive that. No? I guess I am trying to figure out if that example will be a good start for a beginner then switch to your implementation when I need more “power”? Also, are meta tables an issue for fast arcade games (versus procedural programming) in term of speed? (Table access) OR this is non issue? Sorry for maybe the stupid questions! Thanks. Mo

It’s up to you, ideally any implementation would include inheritance as that’s one of the fundamental concepts.  As far as I know, there are no issues with performance w/ Meta tables, be more concerned about your functions being profiled.

Hi guys, In addition to the excellent ArdentKid OOP tutorials, I am also looking to the great jetpack template from Glitch Games that you can find here:http://forums.coronalabs.com/topic/18035-free-jetpack-joyride-template/page-2?hl=jetpackI have looking at it and it seems simple enough (and the template works beautifuly!) but I do not know enough about OOP to know if there is any limitations to it and why one will use that implementation or not. It seems to be using meta tables and event listeners (dispatch and listener) like Ardentkid does. Anything I should know before using either ArdentKid or Glitch Games OOP implementation? Again, I do not know enough to really decide! Thanks for any pointers. Mo

Good find! Looks like it uses some OOP concepts well :slight_smile: But I don’t see too much complexity (no inheritance or polymorphism). Could also use a bit more organization for modularity, but that’s dependent on what you want to do with it.

There are lots of ways to do OOP in LUA, unfortunately with Corona there is no "perfect way’ due to the fact you can’t use meta tables with display objects.  So you have one OOP method for tables and one for display objects, unless you don’t use meta tables, then you can use one way for all, but there are limitations using that way.

Corona is working on a way to offer some way to do prototype style OOP, not sure how good it is or if it also has limitations, just know it is coming out.  Anyway you look at it, Lua is not OOP friendly, so you have to figure out a way that works.  Not everyone needs all features of OOP and can sacrifice things like static members or inheritance to use one way over another.

Thanks guys. I was just curious about that implementation. Good find about inheritance! Did not think about that. I guess for a newbie like me it maybe ok? I kind of like that each object (coins, jetpack…) is in seperate file (the minimum for OOP) and they have all the same structure. Your implementation is definitely more powerful. So I think you are right, it depends on what people are looking for. For me is just a way to get away from procedural programming where the code become so difficult to maintain after not working with it for few months (1000’s lines of code) I may start “slow” with this OOP implementation then later switch to yours. Since I bought your template code, I am covered for the future. What do you think of this plan? @cspence: that will web wonderful if Corona came up with it’s own OOP implementation! I have been looking at an iPad app called Codea ( lua based SDK) and even that app has some OOP features. Can’t wait… Thank you both again:) Mo

I’ve seen Codea, I have no interest in coding on an iPad.  That would be extremely painful.

I’m not sure how well the implementation would work (Corona OOP lib) but I have been talking with a few people and I really think it is something that would be super helpful.

Actually you don’t have to anymore since now they have “Air Coding” where you code on your PC and the code is transmitted to your iPad in real time through wifi. Very cool! But the point was simply that even Codea has some simple OOP which make programming easier. Indeed, it will be wonderful to have a Corona OOP! Thanks again guys for all the help. Mo

[quote name=“ArdentKid” post=“194412” timestamp=“1374536372”]Good find! Looks like it uses some OOP concepts well :slight_smile: But I don’t see too much complexity (no inheritance or polymorphism). Could also use a bit more organization for modularity, but that’s dependent on what you want to do with it.[/quote] Do you mean that the way the jetpack template implement OOP will not allow inheritance? Is inheritance super important in small/medium size game design? I will think that encapsulation is the first critical thing and it seems that example acheive that. No? I guess I am trying to figure out if that example will be a good start for a beginner then switch to your implementation when I need more “power”? Also, are meta tables an issue for fast arcade games (versus procedural programming) in term of speed? (Table access) OR this is non issue? Sorry for maybe the stupid questions! Thanks. Mo