Love Object Orientated Programming

General discussion about LÖVE, Lua, game development, puns, and unicorns.
mager1794
Prole
Posts: 5
Joined: Wed Sep 01, 2010 3:18 am

Love Object Orientated Programming

Post by mager1794 »

I've decided to assign myself the project of developing Object Orientated classes (theoretically) for the Love 2D Engine. I believe that all games should be done with Object Orientation because it usually just makes the tasks a thousand times easier. To create what would be a class in lua you actaully use a table. And define it as the name of what you want your class. Then you simply write a function to create your class and a more functions to be used with it. Surprisingly simple. But at the same time - strangely harder than object orientation in most other langauges (IMO).

My first class that i've developed was my Image class.

I proceeded to derive from that class (once again theorectically) to make my sprite class and replicate the 'Hamster Ball' only without key movement. it just rolls off the screen. I hope to work to create even more classes and spread my classes throughout the community.

The Code Below Is Still Under Testing!

Code: Select all

-- Love OO Library Class Sets

Image = {}                   -- Create a table to hold the class methods
function Image:Create(filepath)  -- The constructor
  local object = { img = love.graphics.newImage(filepath) }
  setmetatable(object, { __index = Image })  -- Inheritance
  return object
end
function Image:Draw(x,y)
  love.graphics.draw(self.img,x,y)
end

-- Sprite

Sprite = {}                   -- Create a table to hold the class methods
function Sprite:Create(filepath)  -- The constructor
  local object = { img = Image:Create(filepath), x = 0,y = 0 }
  setmetatable(object, { __index = Sprite })  -- Inheritance
  return object
end
function Sprite:Update(xacc,yacc)
  self.x = self.x+xacc
  self.y = self.y+yacc
end


-- Using this as a test.

-- Tutorial 1: Hamster Ball
-- Add an image to the game and move it around using
-- the arrow keys.
-- compatible with löve 0.6.0



function love.load()
   hamster = Sprite:Create("ball.png")
end

function love.update(dt)
   hamster:Update(1,1)
end

function love.draw()
   hamster.img:Draw(hamster.x,hamster.y)
end
To those of you who don't know much about Object Orientated Programming this probably looks longer and unnecessary but with the use of objects you can create new functions that will work for themselves and add them multiple times without creating a million new definitions.

ex. I could have a table filled with my Sprite Class and just run through the table rather than each sprite class over and over.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Love Object Orientated Programming

Post by bartbes »

mager1794 wrote:To those of you who don't know much about Object Orientated Programming this probably looks longer and unnecessary but with the use of objects you can create new functions that will work for themselves and add them multiple times without creating a million new definitions.
Oh wow, even to me it looks a lot longer and unnecessary.
(not because I don't know OOP, actually, I have even written some class system for lua)
mager1794
Prole
Posts: 5
Joined: Wed Sep 01, 2010 3:18 am

Re: Love Object Orientated Programming

Post by mager1794 »

Object Orientation Always looks longer unless you make an actual game. Simple demos always make it look unnecessary.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Love Object Orientated Programming

Post by vrld »

Why is

Code: Select all

img = Image:Create("path/to/image.png")
img:draw(100,100)
better than

Code: Select all

img = love.graphics.newImage("path/to/image.png")
love.graphics.draw(img, 100, 100)
?
Apart from that it's marginally shorter to write I don't see the benefit of the overhead involved.

OO in games is only useful when you encapsulate data and non-trivial behavior, like your Sprite does.

There are actually a number of OO libraries for LOVE (see http://love2d.org/wiki/Category:Libraries) and if none suits your taste, you can take a look at Programming in Lua which devoted an entire chapter to OOP.


Also:
How do you handle inheritance?
Do you support multiple inheritance?
How will you handle polymorphism?
Will you support private members (and if yes: why)?
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Love Object Orientated Programming

Post by bartbes »

The length I was talking about is simply about not having a system, hence always needing to define a Create method, that stuff sucks.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Love Object Orientated Programming

Post by Robin »

The one thing (well, one of the things) I find odd is

Code: Select all

hamster.img:Draw(hamster.x,hamster.y)
It seems rather un-OO-y (if that makes sense).
Why not create a Sprite.Draw function that makes

Code: Select all

hamster:Draw()
equivalent to above?
Help us help you: attach a .love.
mager1794
Prole
Posts: 5
Joined: Wed Sep 01, 2010 3:18 am

Re: Love Object Orientated Programming

Post by mager1794 »

You guys seem rather unsupportive of this project so i think i'll just keep it to myself til i get farther down the road.


And i didn't see any Object Orientation Librarys
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Love Object Orientated Programming

Post by nevon »

mager1794 wrote:And i didn't see any Object Orientation Librarys
Here are three:

SECS - http://love2d.org/wiki/Simple_Educative_Class_System
HUMP - http://vrld.github.com/hump/#class.lua
MiddleClass - http://love2d.org/wiki/MiddleClass
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Love Object Orientated Programming

Post by bartbes »

mager1794 wrote:You guys seem rather unsupportive of this project so i think i'll just keep it to myself til i get farther down the road.
That sounds harsh, granted we didn't overload you with compliments, but we're just pointing out the stuff that strikes us as odd.
mager1794
Prole
Posts: 5
Joined: Wed Sep 01, 2010 3:18 am

Re: Love Object Orientated Programming

Post by mager1794 »

Its not meant to be harsh and only 1 person pointed out what was odd. The others questioned the point of it. I'm just trying to provide something to the community til I finally get my C# compiler back (i have slow interwebz) then i intend to make an IDE, just needed something to keep my focus til then.



Just to clarify - I intend to build a bunch of classes. Not just this image class as an example. I will hopefully create a bunch of functions and classes for unlimited uses. Im not just trying to show OOP
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests