The following Lua code:
define class Cell Cell = {xPos = 0, yPos = 0, …..} local Cells = { {}, {} } function Cell:new (o, xPos, yPos) o = o or {} setmetatable(o, self) self.__index = self self.xPos = xPos self.yPos = yPos …. return o end -- init matrix of cell class instances for i = 1, 10, 1 do Cells[1] = {} for j = 1, 20, 1 do Cells[i][j] = Cell:new(nil, i, j) end end As a result of executing this code, the values of the class fields in the matrix are set to the following values:
Cells[1][1].xPos = 10 Cells[1][1].yPos = 20 … Cells[1][2].xPos = 10 Cells[1][2].yPos = 20 … Cells[10][20].xPos = 10 Cells[10][20].yPos = 20 Но я ожидал такой результат: Cells[1][1].xPos = 1 Cells[1][1].yPos = 1 Cells[1][2].xPos = 1 Cells[1][2].yPos = 2 What am I doing wrong? I tried different options, but did not get the expected result.
Thanks in advance for the hint
Cells[1] = {}- I assume that the indexiwas thought here, and not one. - aleksandr barakin