Lua OO Library - bmclass

Showcase your libraries, tools and other projects that help your fellow love users.
bmzz
Prole
Posts: 16
Joined: Fri May 06, 2011 9:21 am

Lua OO Library - bmclass

Post by bmzz »

i have no idea about naming =p...
so i called it bmclass

download
bmclass.lua
(2.98 KiB) Downloaded 160 times
Example1 - basic

Code: Select all

 
class = require("bmclass")
 
a = class("a")
 
function a:init(attrs)
    self.x = attrs.x
    self.y = attrs.y
    
    print("for init")
end
 
function a:info()
    print(tostring(self) .. " : Hello!!!")
    print(self.x)
    print(self.y)
end
 
b = a("b", {x = 100, y = 200}) -- output "for init"
b:info()
-- output
-- b : Hello!!!
-- 100
-- 200

print(b.x) -- output "100"


Example2 - Multiple inheritance

Code: Select all

 
class = require("bmclass")
 
a = class("a")
b = class("b")
c = class("c", a, b) -- superclass a and b
 
function a:init() print("a init") end
function b:init() print("b init") end
function c:init()
    c:next(self, "init") -- next to the superclass
    print("c init")
end
 
function a:hello1() print("Hello1") end
function b:hello2() print("Hello2") end
function c:hello3() print("Hello3") end
 
d = c("d")
-- output
-- a init
-- b init
-- c init
 
d:hello1() -- output "Hello1"
d:hello2() -- output "Hello2"
d:hello3() -- output "Hello3"

any idea?

thanks
Last edited by bmzz on Thu May 26, 2011 1:36 pm, edited 1 time in total.
User avatar
ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

Re: Lua OO Library - bmclass

Post by ishkabible »

dose it support data member inheritance? that's the things that always gets me about inheritance in Lua.
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Lua OO Library - bmclass

Post by Kadoba »

ishkabible wrote:dose it support data member inheritance? that's the things that always gets me about inheritance in Lua.
Can't you just copy all of the members in a for loop?

Code: Select all

for k,v in pairs(parent)
   if type(v) == "number" or type(v) == "string" then
      child[k] = v
   end
end
or maybe I'm missing something.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Lua OO Library - bmclass

Post by Robin »

Kadoba wrote:Can't you just copy all of the members in a for loop?
Metatables are better suited for that. (Besides, you are missing booleans. ;))
Help us help you: attach a .love.
User avatar
ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

Re: Lua OO Library - bmclass

Post by ishkabible »

also, i don't want to have to do that, i want the OO mechanism to do that for me.

Mod edit: There's a delete button, you know.
bmzz
Prole
Posts: 16
Joined: Fri May 06, 2011 9:21 am

Re: Lua OO Library - bmclass

Post by bmzz »

ishkabible wrote:also, i don't want to have to do that, i want the OO mechanism to do that for me.

Mod edit: There's a delete button, you know.
Hi, look at the Example1 that i modified it.
It support data member inheritance.
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: Lua OO Library - bmclass

Post by Ensayia »

Oh look, another OO library.

We have like what, 5 now?
User avatar
ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

Re: Lua OO Library - bmclass

Post by ishkabible »

lol, i guess. i typically just role my own classes in Lua. i have a working template that i use

Code: Select all

--includes go here
Widget = require "widget"

--declare type and metatable, both as local so they don't interfer with other things
local Lable = {}
local mt = {__index = Lable}

--inhertance goes here
setmetatable(Lable, {__index = Widget})

--cache any namespaces
local getColor = love.graphics.getColor
local setColor = love.graphics.setColor
local setFont = love.graphics.setFont
local drawRect = love.graphics.rectangle
local drawPrintf = love.graphics.printf
local getFont = love.graphics.getFont

--create the constructor here, organize the data members neatly somehow
function Lable:new(x,y,w,h,stext)
	return setmetatable( {x, y, w, h,
						 Handle = nil,
						 text = stext,
						 algin = "left",
						 font = getFont(),
						 color = {getColor()},
						 
	},mt)
end

--functions go here

function Lable:Draw()
	setColor(self.color)
	setFont(self.font)
	drawPrintf(self.text, self:GetX(), self:GetY(), self:GetW(), self.algin)
end

function Lable:__tostring()
	return self.text
end

function Lable:Type()
	return "Lable"
end

function Lable:SetText(text)
	self.text = text
end 

function Lable:SetColor(r, b, g, a)
	if type(r) == "table" then
		self.color = {unpack(r)}
	else
		self.color = {r, b, g, a}
	end
end

function Lable:GetFont()
	return self.font
end

function Lable:SetFont(font)
	self.font = font
end

function Lable:SetAlign(a)
	self.algin = a
end

function Lable:GetColor()
	return unpack(self.color)
end

function Lable:GetText()
	return self.text
end

--return the type that was created
return Lable
the downside is that i have to re-declare members from inherited types. i could create a something to do it for me but im lazy i guess.

edit:
im gonna make another and call it "YAOOL", Yet Another OO Lib :)
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Lua OO Library - bmclass

Post by BlackBulletIV »

Ensayia wrote:Oh look, another OO library.
Is that a bad thing? It's all good messing around with Lua.
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: Lua OO Library - bmclass

Post by Ensayia »

BlackBulletIV wrote:
Ensayia wrote:Oh look, another OO library.
Is that a bad thing? It's all good messing around with Lua.
It's not necessarily a bad thing at all, it's just that this community is far more interested in reinventing the wheel than making any actual games with this game framework.

I've already gotten my karma dinged for pointing out the obvious, so I'll drop the issue now.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 8 guests