Re: classes in Lua???
Posted: Thu Apr 09, 2009 10:07 pm
You shouldn't actually have to recurse anything. When you do something like 'bnvar = Button.new()', you're making an instance of Button. The values you initialize in Button.new are private to bnvar but bnvar has been hooked up to Button (via metatable) so if you have something like Button:Draw() or Button:SetText(), you can execute those functions for bnvar like so: 'bnvar:SetText(mytext)'.
The Button:SetText function might look like this:
Feel free to take a look at the gui.lua module in Militia Defense, or even the units in gamedata.lua. It can get pretty nasty as I get a pretty lazy attitude when I'm working in lua, but the class concept and implementation should be sound.
The Button:SetText function might look like this:
Code: Select all
function Button:SetText(newtext)
self.text = newtext;
end