Without further ado, Download and read source. Then run demo.
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.
A Lua Class Implementation using meta-tables by appleide. 3/ 04/ 2009Love Classes implementation
Re: Love Classes implementation
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)
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Love Classes implementation
You probably haven't read the source. I think it is supposed to work like that.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>
Help us help you: attach a .love.
Re: Love Classes implementation
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.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>
Re: Love Classes implementation
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:
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?
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
Re: Love Classes implementation
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.
Example code:
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
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)
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Love Classes implementation
Muhahahahahaha. You could add my special secs compatibility var though.
EDIT: love the copyright notice btw
EDIT: love the copyright notice btw
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Love Classes implementation
It might not be SECS, but it sure is SECS-y.bartbes wrote:Muhahahahahaha. You could add my special secs compatibility var though.
Help us help you: attach a .love.
Re: Love Classes implementation
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...
Who is online
Users browsing this forum: No registered users and 2 guests