fumanshoo Posted February 7, 2013 Share Posted February 7, 2013 So I have a question about classes... I hear this almost every time I open my browser to research anything about programming and I just want to know a very very basic definition of what it is and how it can be used in Lua... Quote Link to comment Share on other sites More sharing options...
Rick Posted February 7, 2013 Share Posted February 7, 2013 Lua has to "fake" classes and it'll most likely confuse you to see how you do a class in Lua because of it. In C++ a class looks like: class Car { private: Color color; // only visible inside this class protected: public: // visible inside this class and any class the derives from this class void Drive() {} // visible outside of this class }; Car myCar; myCar.Drive(); Quote Link to comment Share on other sites More sharing options...
fumanshoo Posted February 7, 2013 Author Share Posted February 7, 2013 So basically, it's an entity's properties but... condensed? Quote Link to comment Share on other sites More sharing options...
Rick Posted February 7, 2013 Share Posted February 7, 2013 Properties and functionality grouped together (variables & functions). Humans tend to like to think of objects and objects have properties to them and they do things. Everything is an objects really when you think about it. You can describe most everything with a class. 1 Quote Link to comment Share on other sites More sharing options...
fumanshoo Posted February 7, 2013 Author Share Posted February 7, 2013 So even things like particle effects and things in the frameworks? I now understand what you are saying, but I just want to know to what extent you can use this. Quote Link to comment Share on other sites More sharing options...
Rick Posted February 7, 2013 Share Posted February 7, 2013 You can use this to extreme extents When I program in C++ everything is an objects. To name a few to get an idea: Camera Player Enemy Weapon Ammo Rectangle Point Animation Action the list goes on and on. Everything in your entire game can be described with objects. Good practice has you keeping objects fairly small and only store the info and functionality it needs. You don't want massive objects that do everything under the sun. You want more smaller objects that work together. Quote Link to comment Share on other sites More sharing options...
fumanshoo Posted February 7, 2013 Author Share Posted February 7, 2013 And last question: Is it practical to use this often. In other words, if I were to show a master programmer ( never gonna happen because of how terrible I am ) my code and he saw that almost everything was classes, would this look stupid or no? Quote Link to comment Share on other sites More sharing options...
Rick Posted February 7, 2013 Share Posted February 7, 2013 No, it wouldn't look stupid. It's what they would expect (given the class designs were logical) if using a language that supports classes like C++ or Java to name just a couple. Quote Link to comment Share on other sites More sharing options...
fumanshoo Posted February 7, 2013 Author Share Posted February 7, 2013 Ok, thank you! Quote Link to comment Share on other sites More sharing options...
Road Kill Kenny Posted February 7, 2013 Share Posted February 7, 2013 It would actually look stupid if you didnt use classes. at least in C++ 1 Quote STS - Scarlet Thread Studios AKA: Engineer Ken Fact: Game Development is hard... very bloody hard.. If you are not prepared to accept that.. Please give up now! Link to comment Share on other sites More sharing options...
fumanshoo Posted February 7, 2013 Author Share Posted February 7, 2013 I see. In that case, it is time for me to research! I don't want to look like a fool next time I post up some code... Quote Link to comment Share on other sites More sharing options...
Rick Posted February 7, 2013 Share Posted February 7, 2013 You are better off learning Java because it forces you to make EVERYTHING a class. C++ doesn't have this requirement so it's probably not the best language to learn about classes with. I suggest coding in Java for a couple months so you are used to making everything a class, then come over to C++ and learn it's slight differences in how it handles classes, but at least you'll be in the good habit of making everything a class. Quote Link to comment Share on other sites More sharing options...
ChrisMAN Posted February 7, 2013 Share Posted February 7, 2013 Lua does OO very differently. It's OO system was modeled after Self. http://en.wikipedia.org/wiki/Self_(programming_language) Its method of oo uses the caller of the method as the 'this' or as lua calls 'self'. even though the owner of the function can be a totally different object. if you have ever done OO in javascript you will feel this pain if you used to the C family languages. There are basically 2 ways of doing classes in lua. You can use meta tables or you can literally copy methods from one object to another. The copying method is often called a 'mixin'. There is no way around reading luas documention on tables and meta tables. To make matters worse lua has gained much of its popularity from it being the language used for the user interface system in WoW. That means that google is half as effective. If you use metatables for your 'classes' you are using something much like prototypical inheritence This is what got me started with metatables as classes http://lua-users.org/wiki/SimpleLuaClasses If you want to use a mixin you can use a function like this function mixin(dest, src) assert(dest) assert(src) for key, value in pairs(src) do dest[key] = value end end If you are writing code for someone else to use. You should use mixins because they don't depend on metatables to work. I am sorry for not explaining this better. Quote Link to comment Share on other sites More sharing options...
ChrisMAN Posted February 7, 2013 Share Posted February 7, 2013 You are better off learning Java because it forces you to make EVERYTHING a class. C++ doesn't have this requirement so it's probably not the best language to learn about classes with. I suggest coding in Java for a couple months so you are used to making everything a class, then come over to C++ and learn it's slight differences in how it handles classes, but at least you'll be in the good habit of making everything a class. It is about to gain blocks, and dynamic typing. It will almost be a on parity with c#. Seriously though... the inability to use procedual forms of programming... I love procedural programming. OO is the devil in diguise. There was so much knowledge that was built up through the 70s and 80s and it was destroyed by .net and java. It wasn't until about 5 years ago where the renascence picked up again. Try reading up on logic programming. It is pretty wild how they solve things. I a few years out from learning a functional language. I hear you have to wear a sweater to be a functional programmer. Clojure is picking speed. Soon it will have alternative VMs to the jvm. Rust lang http://www.rust-lang...ang.org/ This language has epic potential. It has bindings to sdl. Too much to learn. So little time... 1 Quote Link to comment Share on other sites More sharing options...
paramecij Posted February 7, 2013 Share Posted February 7, 2013 If you want to really impress the master programmer, write half your code as inline asm Seriously though, writing everything as classes isn't the correct way ..or even a better way, it wont make you look any cooler, nor will your programs work any better. OOP has advantages and disadvantages like everything else in life, the real cool thing is being aware of all the different tools and techniques available to you and to know when and how to use them to your advantage. I encourage you to learn more about OOP, but don't take it as the only way, and you'll be good.. 3 Quote Link to comment Share on other sites More sharing options...
Rick Posted February 7, 2013 Share Posted February 7, 2013 I'd argue that it makes your program more maintainable and extendable which are some of the main reasons it exists. We have to learn from our elders who created OOP for a reason. 2 Quote Link to comment Share on other sites More sharing options...
Guest Red Ocktober Posted February 11, 2013 Share Posted February 11, 2013 the universe is objects... therefore, it must be the product of object oriented programming... if you stop to take a look at it, oop is actually organic, cosmic one could say... just a minute... just a minute... I'm picking up a fault in the AE-35 unit... --Mike Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.