looping through a metatable
Posted: Tue May 31, 2022 9:35 am
hi so ive been trying to learn more about lua and eventually stumbled accross inheritance. i am familier with C++ and have used inheritance in C++ and was excited to try it out with metatables in lua. I did eventually learn metatables and i defenitly need to learn more but I think i got the basic concept down. what im trying to do is not have to manually enter in everythings love callback (load(), update(dt), and draw()). i did this with metatables and got things to print. but i wanted this to be automatic. I tried looping over my the metatable but it litterally does nothing. i want to go, for every :load() in a table that inherits from obj, call that function inside of the love.load() callback. here is all my code
please if anyone has any idea how to help then please tell me!
Code: Select all
obj = {}
function obj:new (o)
o = o or {}
setmetatable(o, self)
--obj.push(self)
self.__index = self
return o
end
function obj:load(b)
print("jis")
--return b
end
new = obj:new()
function new:load()
print("new")
end
function love.load()
--obj:load()
for i,v in ipairs(obj) do
v:load()
end
end