Page 1 of 1

looping through a metatable

Posted: Tue May 31, 2022 9:35 am
by Seth144k
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

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
please if anyone has any idea how to help then please tell me!

Re: looping through a metatable

Posted: Tue May 31, 2022 10:13 am
by pgimeno
Since you're familiar with C++, think about how you'd do it with C++.

If you want your obj:load(), obj:update() etc. to be invoked automatically, you need to know where your instances are. They are not in the obj table, which is the class, so you need a mechanism to locate them. You could have an object pool with all your objects so you have them located, but then you have to be careful to add an object to the pool every time it is created, and remove it from the pool when destroyed. Then only invoke load() for those that have a load method, update() for those that have an update method, and so on.

Re: looping through a metatable

Posted: Tue May 31, 2022 10:47 am
by darkfrei
I am not familiar with metatables, but I would have the list of loadings:

Code: Select all

function love.load()
    for i, object in ipairs (objects) do
        object:load()
    end
end
Where "objects" is a list of loading objects :)

Re: looping through a metatable

Posted: Tue May 31, 2022 8:50 pm
by Seth144k
ok so I managed to get a complete system up and running but there is still one problem with it that i dont like. preferrablly if possible i would like to have all of the require statements done automatically. when making a new object you still have to go in main.lua and require the path to that lua file otherwise nothing will happen. in main.lua is there a way to not have to require anything but everything still work? or is there a way to have every require done automatically? i would like to have a folder titled objects and just have main.lua automatically require everything in that folder. here is my love project

Re: looping through a metatable

Posted: Wed Jun 01, 2022 7:35 am
by darkfrei