looping through a metatable

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Seth144k
Prole
Posts: 29
Joined: Wed Jan 19, 2022 8:45 pm

looping through a metatable

Post 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!
User avatar
pgimeno
Party member
Posts: 3581
Joined: Sun Oct 18, 2015 2:58 pm

Re: looping through a metatable

Post 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.
User avatar
darkfrei
Party member
Posts: 1184
Joined: Sat Feb 08, 2020 11:09 pm

Re: looping through a metatable

Post 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 :)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Seth144k
Prole
Posts: 29
Joined: Wed Jan 19, 2022 8:45 pm

Re: looping through a metatable

Post 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
Attachments
obj.love
(1.07 KiB) Downloaded 54 times
User avatar
darkfrei
Party member
Posts: 1184
Joined: Sat Feb 08, 2020 11:09 pm

Re: looping through a metatable

Post by darkfrei »

:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Bing [Bot] and 8 guests