Page 2 of 2

Re: Trouble understanding classes

Posted: Sun Dec 07, 2014 9:36 am
by ivan
AlexCalv wrote:I took a short break from this and what partial understanding I had, has faded away. So for the method you posted, do I have to type both code boxes you posted?
The first example (Object) shows how to define a class.
Add your methods to this class like so:

Code: Select all

function Object:set_position_method(x, y)
  self.x = x
  self.y = y
end
AlexCalv wrote:And how do I actually start to interact with these classes.
This is how you create an instance of "Object":

Code: Select all

my_object = Object:create()
my_object:set_position_method(100, 100)
assert(my_object.x = 100 and my_object.y = 100)
The "Container" example shows you how to do "inheritance".

Re: Trouble understanding classes

Posted: Sun Dec 07, 2014 12:12 pm
by s-ol
AlexCalv wrote:
ivan wrote:snip
I took a short break from this and what partial understanding I had, has faded away. So for the method you posted, do I have to type both code boxes you posted? And how do I actually start to interact with these classes. Like, how do I move them, or even draw them?
He is talking about a different way to create and manage classes, not solve your problem. His code doesn't contain anything to draw or even move, he only defines a class "Container" in two different ways.

Re: Trouble understanding classes

Posted: Sun Dec 07, 2014 1:23 pm
by nfey
It seems to me that you have a problem understanding OOP in general, not necessarily OOP applied to Lua.
IMHO, the way classes work in Lua is overcomplicated and the basic OOP principles get lost under a layer of unfriendly syntax.
I'd suggest doing an OOP crash course in a language that's been built purposefully for it. I.e. Java, C#, C++ . If you manage wrapping your head around the concepts, the transition to Lua classes is easier afterwards, imho, compared to the other way around.

Re: Trouble understanding classes

Posted: Mon Dec 08, 2014 12:26 am
by AlexCalv
Yeah I really don't understand this at all. I think I'll just try and avoid using classes. Unless using a library will help me understand it more than from coding it from scratch. Idk. Lua is the first language that I'm learning and I feel like if I were to switch languages to try and learn something like that, it'll mess me up a lot.

Re: Trouble understanding classes

Posted: Mon Dec 08, 2014 5:57 am
by s-ol
AlexCalv wrote:Yeah I really don't understand this at all. I think I'll just try and avoid using classes. Unless using a library will help me understand it more than from coding it from scratch. Idk. Lua is the first language that I'm learning and I feel like if I were to switch languages to try and learn something like that, it'll mess me up a lot.
Avoiding classes isn't a great idea either though, you will have to re-learn a lot of best-practices etc. once you realize how they work.

Re: Trouble understanding classes

Posted: Mon Dec 08, 2014 11:16 am
by nfey
@AlexCalv - I don't want to discourage you in any way, but programming is hard. It's not all about "learning a language" (i.e. a particular syntax). It's about being able to understand both abstract concepts and the practical implementation, but most importantly about a continuous process of documentation and learning, even when you feel like you can't understand anything that you're reading, until it all clicks into place.

In your particuar case, you can't simply avoid learning at least the basics of OOP, because the entire LOVE framework is built on this programming pattern.

I would advise to keep reading OOP tutorials on the internet, wikipedia pages on OOP concepts, maybe to try and get your hands on some programming courses. The best case scenario would be to have a friend explain this to you.

I'd start with looking up what a class is, what an instance is, what an object reference is, what a value type is, what the garbage collector does, what the "self" keyword is and how it works. You can look these things up on google.

I found some Lua OOP programming resources on a quick search, but they don't seem that beginner-friendly (they all kinda assume that you already have applied OOP knowledge from other languages)

http://lua-users.org/wiki/ObjectOrientedProgramming
http://theindiestone.com/forums/index.p ... etatables/
http://www.troubleshooters.com/codecorn/lua/luaoop.htm