Re: Error with table of tables
Posted: Sun May 01, 2022 2:45 pm
Thanks you, i acctually wanted to do like first a perline noise grass layer and then start filling it with dirt and stone, but this will definitly help! Thank you.
Sorry to bother again, but for some reason this simply does nothing, i even looked if it even looped thru there with and if statement in the second for loop, yet nothing happend. The second one for the stones works fine, but always starts from the left.pgimeno wrote: ↑Sun May 01, 2022 11:09 am Not sure I understand. Map is a "y" table (first subindex). Each element of the "y" table contains an "x" table.
If you put the 0 or 1 or 2 or 3 into the "y" table, you will be overwriting one "x" table.
Where do you want the 0 or 1 or 2 or 3 exactly? The most logical place seems to be inside the "x" tables, because if you place it in the "y" table, you're replacing one of the "x" tables with a number (which is the problem you were having). Also, the other function seems to expect the values to be indeed in the "x" tables.
And I don't get how you want them distributed. Do you want each cell different? Do you want to fill the terrain with all the same value, just that value is random from 1 to 4? Do you want them scattered, like rocks or coins?
I'll show you both just in case. You seem to have defined three types: grass (1), dirt (2) and stone (3). Let's imagine that you want each cell to randomly be either grass or dirt, and then have some stones scattered.
To do this, first you iterate over all tables, changing values in them, then you scatter some rocks (probably a percentage of the total number of cells):Code: Select all
-- first, fill each cell at random with either grass or dirt for y, xs in ipairs (map) do for x, value in ipairs (xs) do xs[x] = love.math.random(1, 2) -- a value between 1 and 2 end end -- next, scatter some stones for i = 1, width * height * (5/100) do -- 5% of stone (approx) local y = love.math.random(1, height) local x = love.math.random(1, width) map[y][x] = 3 end
Code: Select all
for y, xs in ipairs (map) do
for x, value in ipairs (xs) do
xs[x] = love.math.random(1, 2) -- a value between 1 and 2
end
end
Code: Select all
map = {{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},}
for y, xs in ipairs (map) do
for x, value in ipairs ( xs ) do
map[y][x] = love.math.random(1, 3) --will be doing some 1d perlin noise!
print("done")
end
end
Sorry, I didn't realize you didn't have anything inside the horizontal tables. They need to be filled with something for ipairs() to do its job.NoreoAlles wrote: ↑Mon May 02, 2022 6:23 pm Sorry to bother again, but for some reason this simply does nothing, i even looked if it even looped thru there with and if statement in the second for loop, yet nothing happend. The second one for the stones works fine, but always starts from the left.