Page 1 of 1

Love Classes implementation

Posted: Thu Apr 02, 2009 1:07 pm
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.

Re: Love Classes implementation

Posted: Thu Apr 02, 2009 8:34 pm
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>

Re: Love Classes implementation

Posted: Thu Apr 02, 2009 8:59 pm
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.

Re: Love Classes implementation

Posted: Fri Apr 03, 2009 2:50 am
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.

Re: Love Classes implementation

Posted: Fri Jan 29, 2010 10:18 pm
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?

Re: Love Classes implementation

Posted: Thu Feb 04, 2010 7:30 am
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)

Re: Love Classes implementation

Posted: Thu Feb 04, 2010 2:53 pm
by bartbes
Muhahahahahaha. You could add my special secs compatibility var though.

EDIT: love the copyright notice btw

Re: Love Classes implementation

Posted: Thu Feb 04, 2010 2:59 pm
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. ;)

Re: Love Classes implementation

Posted: Fri Feb 05, 2010 2:49 am
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...