how to find out all properties of an object in lua?

前端 未结 3 929
别那么骄傲
别那么骄傲 2021-02-20 07:15

Is there any way of getting all the non-nil parameters / properties of an object? I found this: getmetadata(self.xxxx) and i am looking for something like: ge

3条回答
  •  你的背包
    2021-02-20 07:26

    I wrote my own printObject code.. here it is

    function printObj(obj, hierarchyLevel) 
      if (hierarchyLevel == nil) then
        hierarchyLevel = 0
      elseif (hierarchyLevel == 4) then
        return 0
      end
    
      local whitespace = ""
      for i=0,hierarchyLevel,1 do
        whitespace = whitespace .. "-"
      end
      io.write(whitespace)
    
      print(obj)
      if (type(obj) == "table") then
        for k,v in pairs(obj) do
          io.write(whitespace .. "-")
          if (type(v) == "table") then
            printObj(v, hierarchyLevel+1)
          else
            print(v)
          end           
        end
      else
        print(obj)
      end
    end
    

    This is the opposite approach then the post before used. Go through all key value pairs in the table. If the value of one index is a table, go through this table. This solution will not get the upwards metatables like the other post did

提交回复
热议问题