Trouble understanding classes

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Trouble understanding classes

Post 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".
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Trouble understanding classes

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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
nfey
Citizen
Posts: 63
Joined: Tue Feb 18, 2014 9:50 am
Location: Romania

Re: Trouble understanding classes

Post 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.
User avatar
AlexCalv
Prole
Posts: 49
Joined: Fri Aug 08, 2014 7:12 pm
Contact:

Re: Trouble understanding classes

Post 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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Trouble understanding classes

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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
nfey
Citizen
Posts: 63
Joined: Tue Feb 18, 2014 9:50 am
Location: Romania

Re: Trouble understanding classes

Post 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
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 41 guests