How to initialize a table with tables in Lua?
A friend and I are trying to write a sweeping program in Lua using the Löve framework. For now, the code has to check if the checkbox (cell) is checked and then draw it. We are new to Lua, the program now has the disadvantage that it only works in the lower right corner.
UPDATE: I can now see that the values of the initialized GameBoard have the same value (i.e. GameBoard[1]
before GameBoard[150]
- all the same cells).
Here's the code:
conf.lua
has certain global variables:
function love.conf(t)
-- Global variables.
CELL_SIZE = 40
NUM_ROWS = 15
NUM_COLS = 10
STATS_HEIGHT = 100
AMOUNT_OF_CELLS = NUM_ROWS * NUM_COLS
GRID_WIDTH = 400
GRID_HEIGHT = 700
end
Here, the corresponding error code main.lua
(it is wrong in the method of download, which GameBoard
is filled with Cell
s.
-- The Cell table is used for every individual square on
-- the gameboard
Cell = {}
-- The Gameboard (150 Cell objects)
GameBoard = {}
-- The function new belongs to Cell and spawns a new object (a table)
-- with the same attributes as Cell.
function Cell:new(i, j)
-- each cell knows:
-- its x- and y-coordinates.
self.x_min = (i-1) * CELL_SIZE
self.x_max = (CELL_SIZE-1) + (i-1) * CELL_SIZE
self.y_min = STATS_HEIGHT + (j-1) * CELL_SIZE
self.y_max = STATS_HEIGHT + (CELL_SIZE-1) + (j-1) * CELL_SIZE
-- if it is a mine (determined with random number generator)
isMine = (math.random(1, 8) % 8 == 0) -- Roughly 0.15 (1/7) times true (is a mine)
self.isMine = isMine
-- do not check the mine initially
self.checked = false
-- return the cell object
return self;
end
-- love.load is a love-function that is called once when the game
-- starts.
function love.load()
-- The index of the cell on the GameBoard (ranging from 1 to 150)
local index = 1
-- Build a two dimensional table of Cell-objects
for i = 1, NUM_COLS, 1 do
for j = 1, NUM_ROWS, 1 do
GameBoard[ index ] = Cell:new( i, j )
index = index + 1
end
end
end
As a result, all fields have the values of the lower block with the index 150 (the last with NUM_ROWS * NUM_COLS
= 150). All elements ( Cells
) of table ( GameBoard
) have the same x and y values set in the method Cell:new
.
It would be helpful if someone could tell us how to properly initialize the table and access it.
source to share
In the function Cell:new
, self
there is the table itself Cell
, so you return the same table each time.
An easy fix is to create a new table instead:
function Cell:new(i, j)
local t = {}
t.x_min = (i-1) * CELL_SIZE
--omit the rest
return t;
end
For future improvement, you may be interested in another way that the prototype implements:
function Cell:new(i, j)
local o = {}
setmetatable(o, self)
self.__index = self
self.x_min = (i-1) * CELL_SIZE
--omits rest
return o;
end
Read PiL: Object Oriented Programming for more details .
source to share