Vector2 Class - OOP in love2d?

Janywer
Prole
Posts: 9
Joined: Tue Aug 20, 2013 11:51 am

Vector2 Class - OOP in love2d?

Hi guys, I was aware of love2D for quite some time now but never found any time (well, to be honest I was just lazy) to get into it and perhaps make a few of games with it.

However, a friend showed me some awesome stuff yesterday and I finally got myself to give it a try
I'm usually striving to replicate a OOP behavior in languages which aren't having a built-in object system, therefore I started with making a Vector2 class.

https://github.com/BAUER102/love2d-clas ... ector2.lua

Would you

1.) Mind to give me some feedback on it?
And 2.) Show me some other attempts of building a object system in love2D?

pauljessup
Party member
Posts: 188
Joined: Wed Jul 03, 2013 4:06 am

Re: Vector2 Class - OOP in love2d?

I like using MiddleClass for OOP myself.

https://github.com/kikito/middleclass/wiki

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Vector2 Class - OOP in love2d?

I would normally just write it like normal, whithout using libs like MiddleClass or SECS. It doesn't takes anywhat significant time or effort anyway.

pauljessup
Party member
Posts: 188
Joined: Wed Jul 03, 2013 4:06 am

Re: Vector2 Class - OOP in love2d?

I dunno about that man. OOP has some huge benefits that take a bit of working around to do in LUA otherwise. Like mixins, inheritance, multiple inheritance, etc, that can be very powerful tools.

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Vector2 Class - OOP in love2d?

Let me disagree to you with statement about benefits of OOP. It doesn't have any kind of benefit. All it has is a bunch of syntax sugar, period. If you see it as a benefit then I'm not entirely sure you're qualified to tell benefits of this kind of things.

That doesn't mean I don't use OOP though. I never said so. I just don't abuse it; when situation calls for it, I employ some techniques I think are appropriate there. Like in my controls library, I use OOP approach to make it easy for users to handle the states, since Lua provides colon function call syntax anyway. If it wasn't, I'd still used this kind of approach, only I'd have to explicitly declare "self" argument. That's how you do that in C. Also, in C you would use underscore notation rather than dot/colon notation:

Code: Select all

library_class_function ( object, argument )
It doesn't makes much difference at all against using provided syntactical sugar.

Code: Select all

library.class.function ( object, argument )
-- OR
object:function ( argument )
The latter is shorter though, but having to type slightly less doesn't actually makes any difference if you think about it. You could easily achieve exactly the same result just by using shorter names, so this argument doesn't makes sense. Besides, it's not like having to type extra 30% makes anywhat significant difference, since most the time you would think rather than type (and you would often think while you type). "Coding is easy part." The real thing here is that you don't have to know exactly which class object is to call it's function. On the other hand, you would exactly know what class it is in advance before even calling this function, because you expect specifically defined behavior (if you weren't sure you'd implemented class check functons to ensure), so this kind of arugment doesn't makes sense as well. This makes for a conclusion that the whole difference, basically, is having to type slightly less on the one hand, and the possibility of being confused about different classes different implementations of the same named function on the other hand. Not really a benefit, more like a tradeoff with arguable profits.

vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Vector2 Class - OOP in love2d?

pauljessup wrote:1.) Mind to give me some feedback on it?
Looks reasonable to me, though I'd scratch the __index metamethod. But that is just preference.
pauljessup wrote:2.) Show me some other attempts of building a object system in love2D?
There are quite a lot. Around here SECS, Middleclass, hump.class[1], Slither and 30log seem to be the most popular ones. The Lua-users wiki also has a lot to offer.

Anyway, you might want to "unlearn" some of the stuff you know from other languages. It's not that common OO practices from the Java/C#/python/ruby-world(s) are not possible with Lua, it's just that they are often not neccessary. First class functions, proper closures, meta-tables and duck-typing allow elegant solutions where other languages you'd have to use a clunky design pattern.
raidho36 wrote:Let me disagree to you with statement about benefits of OOP. It doesn't have any kind of benefit. All it has is a bunch of syntax sugar, period.
You're wrong. OOP is more than just syntax sugar.

[1] [shameless-plug] also check out hump.vector [/shameless-plug]
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Vector2 Class - OOP in love2d?

Well I know that the idea behind OOP is a whole different story, but as of it is now, OOP is merely a bunch of syntax sugar on top of plain imperative programming. When we'll get to the real OOP then we'll have a talk.

mode7
Prole
Posts: 35
Joined: Tue Aug 21, 2012 5:45 pm
Contact:

Re: Vector2 Class - OOP in love2d?

I wrote a similar vector2 class for love a while ago which also used operator overloading which really looks nice, when you can do:

c = a+b

c.x, c.y = a.x + b.x, b.x + b.y

So I used it for all sorts of stuff, also for longer expressions and had my performance drop significant at some point.
The reason was, I was creating a new instance for every operation - like you. This is much slower than the simple arithmetic and variable assignment in the second example. Do this for 1000 Objects every game loop and you will see your performance drop. Especially when using this library for collision detection or other performance critical things,this will matter.

Janywer
Prole
Posts: 9
Joined: Tue Aug 20, 2013 11:51 am

Re: Vector2 Class - OOP in love2d?

Anyway, you might want to "unlearn" some of the stuff you know from other languages. It's not that common OO practices from the Java/C#/python/ruby-world(s) are not possible with Lua, it's just that they are often not neccessary. First class functions, proper closures, meta-tables and duck-typing allow elegant solutions where other languages you'd have to use a clunky design pattern.
The funfact is that I started replicating an OOP system in lua before I even learned any other language. My only goal is pretty much to only use objects for vectors, colors and the 'map' instance, as those are the only occasions where I think those are useful.
[1] [shameless-plug] also check out hump.vector [/shameless-plug]
Me not likely some of the implementations, srry ;p
So I used it for all sorts of stuff, also for longer expressions and had my performance drop significant at some point.
The reason was, I was creating a new instance for every operation - like you. This is much slower than the simple arithmetic and variable assignment in the second example. Do this for 1000 Objects every game loop and you will see your performance drop. Especially when using this library for collision detection or other performance critical things,this will matter.
Yeah, the fact that it calls the new function each time also kills the performance, plus meta-methods itself aren't the fastest. I'll look at critical situations and use other solutions, if needed. Thanks.

Janywer
Prole
Posts: 9
Joined: Tue Aug 20, 2013 11:51 am

Re: Vector2 Class - OOP in love2d?

Note: I've just updated some parts and got an execution time which is about 30% better.

Check the github.

Who is online

Users browsing this forum: No registered users and 15 guests