assigning table.remove to a variable then using it to print a number
Posted: Fri Jul 21, 2017 7:06 pm
The purpose of code is to randomly place flowers, The full tutorial is here
First, In the last line, Both of "position.y" and "position.x" must be numbers in order to indicate a cell in grid table (or array, doesn't know precisely), But I don't get how did it come by that way.
Second, I've searched in "Programming in Lua, Forth Edition" book and did some google but had never seen something like this before, so, If you were at my position what would you do to deal with this problem otherwise asking someone directly?
Code: Select all
grid = {}
for y = 1, 14 do
grid[y] = {}
for x = 1, 19 do
grid[y][x] = {flower = false}
end
end
local possibleFlowerPositions = {}
for y = 1, 14 do
for x = 1, 19 do
table.insert(possibleFlowerPositions, {x = x, y = y})
end
end
for flowerIndex = 1, 40 do
local position = table.remove(possibleFlowerPositions, love.math.random(#possibleFlowerPositions))
grid[position.y][position.x].flower = true
end
Second, I've searched in "Programming in Lua, Forth Edition" book and did some google but had never seen something like this before, so, If you were at my position what would you do to deal with this problem otherwise asking someone directly?