I'm attempting to create a Lua object like this
Block = {x = 0, y = 0, color = "red"}
function Block:new (x, y, color)
block = {}
setmetatable(block, self)
self.__index = self
self.x = x
self.y = y
self.color = color
return block
end
Then putting several instances of this object into a table in a separate file
blocks = {}
table.insert(blocks, Block:new(0, 0, 'red'))
table.insert(blocks, Block:new(2, 0, 'blue'))
table.insert(blocks, Block:new(1, 1, 'green'))
for i,v in ipairs(blocks) do
print(i,v.x, v.y, v.color)
end
But my output is
1 1 1 green
2 1 1 green
3 1 1 green
How can I get these objects to retain their own instance in the table?