This code fills a 10x10 tilemap with random tiles from 1-9, and stores the tile indices in a two-dimensional table by their x and y coordinates.
function init(self)
local tiles = {}
for y=1,10 do
tiles[y] = {}
for x=1,10 do
local tileIndex = math.random(1, 9)
tiles[y][x] = tileIndex
tilemap.set_tile("#tilemap", "layer1", x, y, tileIndex)
end
end
end
Instead of just storing the tile index, you could put a table with whatever data you would like to have about that tile, or whatever you want.
The basic pair of loops to make a “two-dimensional” table is like this:
local t = {}
for y=1,3 do
t[y] = {} -- Create the "row" table. Don't need this if you are accessing the table that is already created.
for x=1,3 do
t[y][x] = x
end
end
-- You end up with a table like this:
t = {
[1] = { 1, 2, 3 },
[2] = { 1, 2, 3 },
[3] = { 1, 2, 3 },
}
-- I usually loop through Y first so it ends up with this layout.
-- You can do X first so you access it `t[x][y]`, the table will just be "rotated".
-- (...which you will probably never need to think about.)
That’s basically all your tilemap is: a table of tile indices. Tiles aren’t objects, there’s not much to them. They’re basically just an index, and I guess “h-flipped” and “v-flipped” properties, if used.
What are you trying to do that you need the coordinates for?