Jump to content

Classes


fumanshoo
 Share

Recommended Posts

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();

Link to comment
Share on other sites

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.

  • Upvote 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

  • Upvote 1
Link to comment
Share on other sites

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

  • Upvote 3
Link to comment
Share on other sites

Guest Red Ocktober

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...