Page 1 of 2
Lua OO Library - bmclass
Posted: Tue May 24, 2011 2:46 am
by bmzz
i have no idea about naming =p...
so i called it bmclass
download
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
Re: Lua OO Library - bmclass
Posted: Wed May 25, 2011 5:23 pm
by ishkabible
dose it support data member inheritance? that's the things that always gets me about inheritance in Lua.
Re: Lua OO Library - bmclass
Posted: Wed May 25, 2011 7:37 pm
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.
Re: Lua OO Library - bmclass
Posted: Wed May 25, 2011 7:56 pm
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.
)
Re: Lua OO Library - bmclass
Posted: Thu May 26, 2011 12:55 am
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.
Re: Lua OO Library - bmclass
Posted: Thu May 26, 2011 1:41 pm
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.
Re: Lua OO Library - bmclass
Posted: Fri May 27, 2011 7:16 am
by Ensayia
Oh look, another OO library.
We have like what, 5 now?
Re: Lua OO Library - bmclass
Posted: Sat May 28, 2011 3:57 am
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
Re: Lua OO Library - bmclass
Posted: Sat May 28, 2011 8:08 am
by BlackBulletIV
Ensayia wrote:Oh look, another OO library.
Is that a bad thing? It's all good messing around with Lua.
Re: Lua OO Library - bmclass
Posted: Sat May 28, 2011 7:29 pm
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.