Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Object-Oriented Lua Tutorials
Started by ArdentKid Dec 05 2012 01:19 AM

* * * * * 1 votes
51 replies to this topic
[TOPIC CONTROLS]
Page 2 of 3 1 2 3
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#26

LairdGames

[GLOBAL: userInfoPane.html]
LairdGames
  • Contributor

  • 969 posts
  • Corona SDK

Hey! I just saw that you have a sample app using your OOP approach. That's a fantastic news! I am very curious how complex is the code un term of say physics and collision for instance? Does the sample code deals with those type of things? I am very curious on the list of classes that are used and their structures. Can you talk about that? Finally, any chance for a limited time price break? :) Thanks again. Mo

[TOPIC: post.html]
#27

ArdentKid

[GLOBAL: userInfoPane.html]
ArdentKid
  • Enthusiast

  • 51 posts
  • Corona SDK

Hi LaridGames,

The sample code does not use corona's box2d, because it is not a physics-based example. Instead, it uses efficient point-based collision detection. The classes are everything mentioned in the tutorial, along with pause/ resume, and whatever else is mentioned on the page as advertised. Sorry, the price is what it is for now, and I think it's very reasonably priced for the things I've demonstrated :)



[TOPIC: post.html]
#28

LairdGames

[GLOBAL: userInfoPane.html]
LairdGames
  • Contributor

  • 969 posts
  • Corona SDK

Cool! Thanks a lot. Mo

[TOPIC: post.html]
#29

LairdGames

[GLOBAL: userInfoPane.html]
LairdGames
  • Contributor

  • 969 posts
  • Corona SDK

Hi, I went ahead and got the source code and I have to admit I am very excited to look into it and learn how to develop in a more OOP way (if that make sense!) After a quick look I also have to admit it is a little overwhelming! I am sure with your online tutorial and the source code, I will eventually get it. To speed up that understanding I will love 2 things if at all possible: - a little more comments in the source code OR maybe a short working document that would quickly show how the pieces works together? Not about the OOP in general (I think you online tutorial works great for that!) I am talking about the actual game of cat in mouse. I am trying to put my head around it and like I said it is little overwhelming at first. - Another thing I am curious about is how to integrate physics into the game (not really for this game but for future game i would mske using OOP) For instance let say I needed to make the mouse a physic object? How can I go about doing that? In any event, I am very grateful that you decided to make this source code available to us. As you know from my earlier posts, that I have been waiting for this example for a long time! I also understand that time is a limited commodity so I am not looking for something right away but maybe in a future update. For people new to OOP (like me!) who bought the code I think it will help us speed our learning curve! Thanks again. Mo

[TOPIC: post.html]
#30

ArdentKid

[GLOBAL: userInfoPane.html]
ArdentKid
  • Enthusiast

  • 51 posts
  • Corona SDK

Thanks & you're welcome, I'm glad it's helping!

I will spend some time in updating the comments and code a bit (someone else requested sounds as well), and you'll receive an email with the updates. I never really used corona's physics library because I haven't created any physics-based games, but that stuff isn't necessary for OOP, and I don't want to bloat the scope of the tutorial. I would continue with these tutorials and other specific subjects when I find the time, but probably not any time soon. I will also make a private git repo for this project so that you and others can contribute to making it better as well.



[TOPIC: post.html]
#31

LairdGames

[GLOBAL: userInfoPane.html]
LairdGames
  • Contributor

  • 969 posts
  • Corona SDK

Cool, thanks ArdentKid! Yea I will not worry to much about the physics part. As you said it is not really  a OOP issue.

 

 On the other hand, I cannot really tell you how I will appreciate more details or comments on the code itself as it stands right now. I spent pretty much the all day today going line by line to see if I can make sense and because I am new to OOP, it is a tough going!

 

So yes more comments in the source code will be much appreciated.The tutorial really helps. Still they are many places that I have problems with (again because it is so new to me) and love to have more details on some lines in your source code. I am wondering what will be the best way to do this. Obviously since you are selling the source code, I do not want to display it here! I would like to go from module to module (sorry class to class!) and mark where I have difficulty understand why you are doing it that way (usually it is only 1 or 2 lines that are causing me problems).

 

Is sending a support email the best way? Or doing it in this form but only showing the class name and code line number?

 

I am very excited about using your OOP methodology on my next app but it is clear that I need first to understand you source code better.

 

Please let me know.

 

Thanks.

 

Mo  



[TOPIC: post.html]
#32

ArdentKid

[GLOBAL: userInfoPane.html]
ArdentKid
  • Enthusiast

  • 51 posts
  • Corona SDK

Yes, please reply to the email for support. I will do my best to help if it's a simple question, but I also offer tutoring services if you'd like to discuss things in-depth.



[TOPIC: post.html]
#33

LairdGames

[GLOBAL: userInfoPane.html]
LairdGames
  • Contributor

  • 969 posts
  • Corona SDK

Excellent! I will send you an email with some questions about the code as soon as I am done with the review of the code. I will try not to ask stupid questions...I cannot make any promises here:)

 

Thanks,

 

Mo



[TOPIC: post.html]
#34

willupc

[GLOBAL: userInfoPane.html]
willupc
  • Observer

  • 3 posts
  • Corona SDK

Hi Omid,

Your Mouse Run demo has been very helpful in getting up and running with OOP in Lua for Corona SDK. I'm new to Corona and Lua, but have many years of experience in C# development for web and windows applications.  I am currently in the design phase of my first Corona app.  I'd like to have my code modular so I can re-use in future projects.  I found many examples of OOP in Lua and was not sure which approach to take.  Yours seemed the most straight forward with detailed explanations of the OOP concepts in Lua and of course it is geared towards development in Corona SDK.

One thing I noticed while reviewing your demo is that there is a Sound.lua class file but there is no implementation or usage of sounds in your demo.  Is it possible for you to update your demo with some basic sound implementations?  I would really appreciate it.

Thanks,
Will



[TOPIC: post.html]
#35

sunmils

[GLOBAL: userInfoPane.html]
sunmils
  • Contributor

  • 197 posts
  • Corona SDK

Thanks @ ArdentKid for this helpful tutorial. I also purchased already.

 

Just one question about this chunk ...

 

function SceneGame:exitScene(event)
	--Clear all required files from lua's package.loaded

	game:dispatchEvent({name='onGameState', phase='exit'})
	
	local pkgs = package.loaded
	for k,v in pairs(pkgs) do
		local test = string.find(k, 'Demos')
		if (test) then pkgs[k] = nil end
	end
end

 

why do you search for the string 'Demos' ? 



[TOPIC: post.html]
#36

ArdentKid

[GLOBAL: userInfoPane.html]
ArdentKid
  • Enthusiast

  • 51 posts
  • Corona SDK

Haha woops! That's for the ArdentKid mobile app I'm trying to release, which will allow this demo to be played inside the app, and project files to be purchased as an in-app purchase. Since I organized all the required files for the Demo in its own subfolder, I could easily un-require them by searching for the word, 'Demo'. So this was to clean up the scene for the in-app demo. You could just replace it with the following:

 

local pkgs = package.loaded
for k,v in pairs(pkgs) do
	local test = string.find(k, 'Classes')
	if (test) then pkgs[k] = nil end
end

 

 

If all your code is in the "Classes" directory.



[TOPIC: post.html]
#37

sunmils

[GLOBAL: userInfoPane.html]
sunmils
  • Contributor

  • 197 posts
  • Corona SDK

ah, ok. now it makes sense. thanks!



[TOPIC: post.html]
#38

JeffreyQJr

[GLOBAL: userInfoPane.html]
JeffreyQJr
  • Contributor

  • 140 posts
  • Corona SDK

Hi I am really interested in buying the sample code to learn OOP quicker. But I need to know if there is any code in it that teaches how to "destroy" an object created from the class? I am pretty sure I can't just do the below:

 

obj = mouse.new()

obj = nil

 

Because I will not be able to remove the image of the object right?



[TOPIC: post.html]
#39

ArdentKid

[GLOBAL: userInfoPane.html]
ArdentKid
  • Enthusiast

  • 51 posts
  • Corona SDK

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!



[TOPIC: post.html]
#40

JeffreyQJr

[GLOBAL: userInfoPane.html]
JeffreyQJr
  • Contributor

  • 140 posts
  • Corona SDK

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? 



[TOPIC: post.html]
#41

ArdentKid

[GLOBAL: userInfoPane.html]
ArdentKid
  • Enthusiast

  • 51 posts
  • Corona SDK

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.



[TOPIC: post.html]
#42

JeffreyQJr

[GLOBAL: userInfoPane.html]
JeffreyQJr
  • Contributor

  • 140 posts
  • Corona SDK

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 :)



[TOPIC: post.html]
#43

ArdentKid

[GLOBAL: userInfoPane.html]
ArdentKid
  • Enthusiast

  • 51 posts
  • Corona SDK

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).



[TOPIC: post.html]
#44

LairdGames

[GLOBAL: userInfoPane.html]
LairdGames
  • Contributor

  • 969 posts
  • Corona SDK

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=jetpack I 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

[TOPIC: post.html]
#45

ArdentKid

[GLOBAL: userInfoPane.html]
ArdentKid
  • Enthusiast

  • 51 posts
  • Corona SDK

Good find! Looks like it uses some OOP concepts well :) 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.



[TOPIC: post.html]
#46

cspence

[GLOBAL: userInfoPane.html]
cspence
  • Contributor

  • 338 posts
  • Enterprise

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=jetpack I 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

 

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.



[TOPIC: post.html]
#47

LairdGames

[GLOBAL: userInfoPane.html]
LairdGames
  • Contributor

  • 969 posts
  • Corona SDK

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

[TOPIC: post.html]
#48

cspence

[GLOBAL: userInfoPane.html]
cspence
  • Contributor

  • 338 posts
  • Enterprise

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.



[TOPIC: post.html]
#49

LairdGames

[GLOBAL: userInfoPane.html]
LairdGames
  • Contributor

  • 969 posts
  • Corona SDK

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

[TOPIC: post.html]
#50

LairdGames

[GLOBAL: userInfoPane.html]
LairdGames
  • Contributor

  • 969 posts
  • Corona SDK

Good find! Looks like it uses some OOP concepts well :) 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.


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


[topic_controls]
Page 2 of 3 1 2 3
 
[/topic_controls]