尝试在字段'w'(值为nil)上执行算术运算

我正在使用面向对象的 Lua 设置进行工作,并且尽管检查字段不为nil,但我收到错误消息“尝试在字段'w'上执行算术运算(值为nil)”

grid = { w=1, h=1, l=1, tiles = {}}
    function grid:new(obj)
         if obj then o = obj else o = {} end
         setmetatable(o, self)
         self.__index = self
         if o.w and o.h and o.l then
             o.tiles = {tile:new()}
             for i = 2, (o.w*o.l*o.h), 1 do table.insert(o.tiles, tile:new()) end
         end
         return o
    end

因检查字段不为nil而期望正常工作,但由于某种原因,字段o.w、o.l和o.h似乎为nil。

点赞