Page 1 of 2
classes in Lua???
Posted: Tue Apr 07, 2009 4:33 pm
by hertzcastle
hi!
ive been trying to figure out how to do class/OOP type stuff in lua, but my code never seems to work. Ive looked all over the lua docs and none of it really makes any sense, so I'm gonna post my code here in hopes that one of you will rescue my soul:
Code: Select all
brickTable = {}
brick = {
xBrick = 200,
yBrick = 200,
imgBrick = love.graphics.newImage("brick.png"),
draw = doDraw(imgBrick, xBrick, yBrick)
}
brickTable[1] = brick
for i, v in ipairs(brickTable) do
brickTable[i].doDraw()
end
thats sort of a broken down version of what im doing, just imagine load, update and draw in all the right places.
whats wrong with this?
please help!
x
Re: classes in Lua???
Posted: Tue Apr 07, 2009 5:35 pm
by mikembley
Just popping in on this, just to say that isnt how LUA classes work, from the examples ive seen.
And for anyone who knows the answer to hertzcastle query could you detail how LUA classes work in lamens terms or the easiest way possible, ive read the PiL docs about them, But i couldnt understand the flow of how it all works and usually on these docs the terminology is pretty confusing for some!
I'd be deeply greatful
Re: classes in Lua???
Posted: Tue Apr 07, 2009 6:52 pm
by bartbes
Let me do a quick explanation of what is wrong, because if I really want to tell you how lua classes work I'll be typing for hours.
First of all, when you're assigning the 'class' you just create a pointer (in C-terms), so all you do is creating a reference to the table instead of cloning it. To clone it there are several ways, first: loop over all indexes and assign their values to the same indexes in the new table, second way: use __index in a metatable, this makes sure that missing keys of the table are looked up in the original table, but when you change them new ones are created.
Next, you need to start working with the self variable, example:
Code: Select all
someTable = {}
someTable.value = 3
function someTable:someFunction() --this is the same as someTable.someFunction(self) (difference is : )
self.value = 20
end
someTable:someFunction()
--someTable.value is now 20, without the function ever knowing it's name, so if it was located in another table it would've set the variable in that table.
I hope this is enough to get you started, because I don't feel like typing a lot.
Re: classes in Lua???
Posted: Tue Apr 07, 2009 9:04 pm
by Gerrit
I'm also not in the mood to write much but I'll post you some sourcecode to get you started. Just the concept. This isn't something out of the box as I'm just writing some stuff together to inspire you
Code: Select all
bricks = {}
function bricks:new( x, y, image )
local brickImage = love.graphics.newImage( imgage )
local newBrick = { image = brickImage, x = x, y = y }
table.insert( bricks, newBrick )
end
function bricks:draw()
table.foreach (bricks , drawBricks)
end
local function drawBricks( _index )
love.graphics.draw( bricks[_index].image, bricks[_index].x, bricks[_index].y )
end
Example usage would be:
Code: Select all
function load()
-- Add a brick with
bricks:new( 200, 200, "brick.png")
end
function draw()
-- Draw all bricks in the draw function
bricks:draw()
end
This would be the simple solution. You can also use "Metatables" and store tables like the one above (with functions) in other (meta)tables. It needs some time to figure out how it's done but it's pretty cool when you learned to use it
PS: Take a look at my image lib source which uses a more oop-like approach like bartbes wrote:
http://love2d.org/forum/viewtopic.php?f=5&t=636
Re: classes in Lua???
Posted: Tue Apr 07, 2009 9:42 pm
by osuf oboys
Hopefully a start of a standardized class library for LÖVE:
http://love2d.org/forum/viewtopic.php?f=5&t=633.
Re: classes in Lua???
Posted: Tue Apr 07, 2009 10:47 pm
by mikembley
Thank you both for the examples, Looks like ill spend a bit of time experimenting with it for a while to get it sunk into my head..
Re: classes in Lua???
Posted: Wed Apr 08, 2009 7:18 am
by bartbes
While I was showering I got a moment of inspiration, therefore I present: The simplest class system I've ever seen (tm)
Code: Select all
--MIT licensed to me (didn't want to fill up this space with the license)
local class_mt = {}
function class_mt:__index(key)
return self.__baseclass[key]
end
class = setmetatable({}, class_mt)
function class:new(...)
local c = {}
c.__baseclass = self
setmetatable(c, getmetatable(self))
if c.init then
c:init(...)
end
return c
end
Example code:
Code: Select all
love.filesystem.require("class.lua") --or whatever you do to load it
myclass = class:new()
myclass.value = 13
function myclass:setvalue(v)
self.value = v
end
object = myclass:new()
object:setvalue(128)
--myclass.value is still 13 and object.value is now 128
anotherObject = object:new()
--anotherObject.value is 128
Hope this suits your needs. (Note that in the above code there is no difference between objects and classes)
EDIT: added constructor
EDIT2: added download, please use that one as that one does contain the license.
Re: classes in Lua???
Posted: Thu Apr 09, 2009 12:12 am
by Alex
This is what I use, which has the advantage of supporting inheritance and is the simplest instance of such that I have seen. I did not write this code, it was originally posted on the Lua User's Wiki. I only modified it slightly so that it would have a smaller memory footprint at the expense of doing away with the ability to check for to see if an instance belongs to a class, which is something I had no interest in anyways.
Code: Select all
function class(base,ctor)
local c = {} -- a new class instance
if not ctor and type(base) == 'function' then
ctor = base
base = nil
elseif type(base) == 'table' then
-- our new class is a shallow copy of the base class!
for i,v in pairs(base) do
c[i] = v
end
end
-- the class will be the metatable for all its objects,
-- and they will look up their methods in it.
c.__index = c
-- expose a ctor which can be called by <classname>(<args>)
local mt = {}
mt.__call = function(class_tbl,...)
local obj = {}
setmetatable(obj,c)
if ctor then
ctor(obj,...)
else
-- make sure that any stuff from the base class is initialized!
if base and base.init then
base.init(obj,...)
end
end
return obj
end
c.init = ctor
setmetatable(c,mt)
return c
end
Examples and the original code in full can be found
here.
Re: classes in Lua???
Posted: Thu Apr 09, 2009 1:40 am
by Tabasco
This is along the lines of what bartbes was talking about. It's how I do it.
Code: Select all
Button = {}
Button.__index = Button
function Button.new(name, text, width, height, x, y, bgcolor, fgcolor, hcolor, func)
local bn = {}
setmetatable(bn, Button)
bn.name = name
bn.text = text
bn.width = width
bn.height = height
bn.x = x + (height / 2)
bn.y = y + (width / 2)
bn.bgcolor = bgcolor
bn.fgcolor = fgcolor
bn.hcolor = hcolor
bn.func = func
bn.hover = false
bn.top = y
bn.left = x
bn.visible = true
return bn
end
function Button:draw()
--reference members here with 'self'
end
Then you make a button like so:
Code: Select all
bnvar = button.new(args)
bnvar:draw()
Re: classes in Lua???
Posted: Thu Apr 09, 2009 8:11 pm
by hertzcastle
ah cool, thanks tabasco! how would i iterate over the objects in a metatable? is it the same as a table? i.e with ipairs?x