Love Classes implementation

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

Love Classes implementation

Post by appleide »

Without further ado, Download and read source. :) Then run demo.
LoveObject.love
(3.95 KiB) Downloaded 314 times
A Lua Class Implementation using meta-tables by appleide. 3/ 04/ 2009
Features:
-- Multiple Inheritance
-- Static variables
-- Static and instance methods
-- Namespaces

You must agree to this license:
LICENSE:
YOU ARE ONLY GRANTED PERMISSION TO USE THIS SOFWARE IF YOU AGREE THE AUTHOR
IS NOT RESPONSIBLE OR LIABLE FOR YOUR USAGE OF THIS SOFTWARE AND ALONG WITH
WHATEVER HARM THAT MAY COMPUTER TO YOUR COMPUTER FROM THIS SOFTWARE ARISING
FROM ANY WAY.

YOU MAY DO ANYTHING WITH THIS SOFTWARE AS LONG AS YOU ABIDE TO THIS LICENSE.

BY USING THIS SOFTWARE YOU AUTOMATICALLY AGREE TO ALL TERMS IN THIS LICENSE.
User avatar
mike
Administrator
Posts: 364
Joined: Mon Feb 04, 2008 5:24 pm

Re: Love Classes implementation

Post by mike »

Code: Select all

[string "main.lua"]:30: Class Demo: printing className of apoint: ChangedClassNameDueToItBeingAStaticVariable
stack traceback:
	[C]: in function 'error'
	[string "main.lua"]:30: in function <[string "main.lua"]:26>
Now posting IN STEREO (where available)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Love Classes implementation

Post by Robin »

mike wrote:

Code: Select all

[string "main.lua"]:30: Class Demo: printing className of apoint: ChangedClassNameDueToItBeingAStaticVariable
stack traceback:
	[C]: in function 'error'
	[string "main.lua"]:30: in function <[string "main.lua"]:26>
You probably haven't read the source. I think it is supposed to work like that.
Help us help you: attach a .love.
User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

Re: Love Classes implementation

Post by appleide »

mike wrote:

Code: Select all

[string "main.lua"]:30: Class Demo: printing className of apoint: ChangedClassNameDueToItBeingAStaticVariable
stack traceback:
	[C]: in function 'error'
	[string "main.lua"]:30: in function <[string "main.lua"]:26>
That shows the static variable of an instance/class is changed successfully. ;) I'm not sure if I should make className a constant instead. Lua doesn't have contants.
User avatar
Clavus
Prole
Posts: 28
Joined: Wed Jan 27, 2010 12:45 pm

Re: Love Classes implementation

Post by Clavus »

Hi, sorry to bump this, but I need some help with tweaking this code. I made a "Vector" class, which holds and x and y value. I want to able to add vectors by just using '+' operator on them. Like so:

Code: Select all

-- LuaObject initialization
game = {}
LoveObject.init(game)

-- Create vector class
game.class("Vector", game.Object)

function game.Vector:init(x, y)
	self.x = x or 0
	self.y = y or 0
	return self
end

-- Example code
local vector1 = game.Vector:new(1,1)
local vector2 = game.Vector:new(2,3)

local newvector = vector1 + vector2
vector1 + vector2 should result in game.Vector:new(vector1.x + vector2.x, vector1.y + vector2.y). Now I know you can define that sort of stuff in metatables using __add, but how do I go about implementing it in your code?
User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

Re: Love Classes implementation

Post by appleide »

Hmm, I'm now using a much simpler implementation of classes derived from (but not equivalent to) bartbes'. LoveObject is overly-complicated in comparison... That said, this version doesn't support multiple inheritance.

Code: Select all

----------------------------------------------------------------------------
--	Copyright (c) 2009 Bart Bes
--	 
--	Permission is hereby granted, free of charge, to any person
--	obtaining a copy of this software and associated documentation
--	files (the "Software"), to deal in the Software without
--	restriction, including without limitation the rights to use,
--	copy, modify, merge, publish, distribute, sublicense, and/or sell
--	copies of the Software, and to permit persons to whom the
--	Software is furnished to do so, subject to the following
--	conditions:
--	
--	The above copyright notice and this permission notice shall be
--	included in all copies or substantial portions of the Software.
--	
--	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
--	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
--	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
--	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
--	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
--	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
--	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
--	OTHER DEALINGS IN THE SOFTWARE.
----------------------------------------------------------------------------


-- Single Inheritance Classes.

local class, class_mt={}, {}
class.__index=class
setmetatable(class, class_mt)

function class_mt:__index(key)
	if rawget(self, "__class") then
		return self.__class[key]
	end
	return nil
end

function class:new(...)
-- Call this to make an instance from a class. ... are init parameters
	local c = {}
	c.__class = self
	c.__index=c
	setmetatable(c, self)
	if self.init then
		c=self.init(c, ...)
	end
	return c
end

function class:subclass(t)
-- Call this to subclass the class. t is a table.
-- i.e class will be the super of t.
	t = t or {}
	t.__class = self
	setmetatable(t, getmetatable(self))
	return t
end

return class
Example code:

Code: Select all

-- class initialization
class = require('class')

-- Create vector class
Vector = class:new()
function Vector:init(x, y)
   self.x = x or 0
   self.y = y or 0
   return self
end

function Vector:__add(vector2)
	return self.__class:new(self.x + vector2.x, self.y + vector2.y)
end

function Vector:__tostring()
	return "("..self.x..", "..self.y..")"
end

-- Example code
local vector1 = Vector:new(1, 1)
local vector2 = Vector:new(2, 3)

local newvector = vector1 + vector2
print(newvector)


Rectangle = Vector:subclass()
function Rectangle:init(x, y, width, height)
	Rectangle.__class.init(self, x, y)
	self.width = width
	self.height = height
	return self
end

function Rectangle:__add(rect2)
	return self.__class:new(self.x + rect2.x, self.y + rect2.y, self.width + rect2.width, self.height + rect2.height)
end

function Rectangle:__tostring()
	return "[("..self.x..", "..self.y.."), ".."("..self.width..", "..self.height..")]"
end

local rect1 = Rectangle:new(1, 1, 10, 10)
local rect2 = Rectangle:new(2, 3, 20, 20)
local rect3 = Rectangle:new(-5, -5, 10, 10)
local newrect = rect1 + rect2 + rect3
print(newrect)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Love Classes implementation

Post by bartbes »

Muhahahahahaha. You could add my special secs compatibility var though.

EDIT: love the copyright notice btw
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Love Classes implementation

Post by Robin »

bartbes wrote:Muhahahahahaha. You could add my special secs compatibility var though.
It might not be SECS, but it sure is SECS-y. ;)
Help us help you: attach a .love.
User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

Re: Love Classes implementation

Post by appleide »

It's META-SECS, since each class happens to be the metatable of its instances. =D I'm not sure its completely compatible with SECS though, so I didn't put the compatibility var...
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests