android - Acces function from constructor in Lua -
i have class in lua. in constructor declare variables (in empty table) , after want acces function of object insert objects in table. code this:
local boxclass = require("box") local surprisebox = {} local surprisebox_mt = { __index = surprisebox } -- metatable function surprisebox.new() -- constructor local object = { boxes = {} } surprisebox:createboxes() print('constructor -> ' .. #object.boxes) --> 0 return setmetatable( object, surprisebox_mt ) end ------------------------------------------------- function surprisebox:createboxes() local box1 = boxclass.new('palo', 'images/chestclose.gif', 'open') local box2 = boxclass.new('moneda', 'images/chestclose.gif', 'open') self.boxes = { box1, box2} end
after access function createboxes() there nothing inside table.
thanks help!
when call surprisebox:createboxes()
, self
parameter still points surprisebox
table, not object
table created. should this:
function surprisebox.new() -- constructor local object = setmetatable( {boxes = {}}, surprisebox_mt ) object:createboxes() print('constructor -> ' .. #object.boxes) end
the key assign metatable before call createboxes()
, call on object
, not surprisebox
.
Comments
Post a Comment