Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through objects of a lua class

Tags:

oop

lua

local card = {x, y, w, h};

    function card:new(o, x, y, w, h)
        local o = o or {};
        setmetatable(o, self);
        self.__index = self;
        self.x = x;
        self.y = y;
        self.w = w;
        self.h = h;
        return o;
    end

How do i loop through objects created by this class?

like image 651
AliYsf Avatar asked Mar 18 '26 02:03

AliYsf


1 Answers

There is no way to do this, unless you add some structure to explicitly track created objects in a table somewhere (probably with "weak" keys to allow them to be garbage collected).

It may be possible to traverse all local and global values to find tables that have __index value pointing to that card table, but it's unlikely to be practical.

like image 167
Paul Kulchenko Avatar answered Mar 19 '26 17:03

Paul Kulchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!