I am messing around with random map generation and right now just to get it down I'm trying to make something that will randomly place 10 objects across 10 tiles with a 50% chance of each tile containing an object. It should check every tile and randomly chose between 0 and 1. If 0 then do nothing and if 1 then place a tile.
for i = 0, 9 do
if love.math.random(0, 1) == 1 then
love.graphics.draw(wall, tilex(i), tiley(0))
end
end
This would randomly place them across the top of the screen, but since this is in the love.draw function, it runs this every tick and the objects jump around. So every frame it re places the objects. How do I make it not do that?
You need to have some way to track that the map has already been generated. Additionally you'll need some way to store the result, so that you can draw it later at will.
-- Somewhere outside love.update and love.draw; you could also make this a global by omitting the "local" if you so wish
local mapTable
-- Inside love.update
if not mapTable then -- check that the table is nil and then generate it
mapTable = {}
for i = 0, 9 do
mapTable[i] = love.math.random(0, 1)
end
end
-- Inside love.draw
if mapTable then -- Only draw the map if it has been generated
for i = 0, 9 do
if mapTable[i] ~= 0 then
love.graphics.draw(wall, tilex(i), tiley(0))
end
end
end
If you then want to re-generate the map, then just set the mapTable to nil.
Also you're better off leaving love.draw only to handle drawing graphics onto the screen, and put any generation or other things that need to be calculated per frame in love.update. For later stuff, if you want to use lua's builtin table functions later it's better to index everything starting from 1 as opposed to 0.
MrFariator wrote: ↑Mon Aug 07, 2017 3:21 am
You need to have some way to track that the map has already been generated. Additionally you'll need some way to store the result, so that you can draw it later at will.
-- Somewhere outside love.update and love.draw; you could also make this a global by omitting the "local" if you so wish
local mapTable
-- Inside love.update
if not mapTable then -- check that the table is nil and then generate it
mapTable = {}
for i = 0, 9 do
mapTable[i] = love.math.random(0, 1)
end
end
-- Inside love.draw
if mapTable then -- Only draw the map if it has been generated
for i = 0, 9 do
if mapTable[i] ~= 0 then
love.graphics.draw(wall, tilex(i), tiley(0))
end
end
end
If you then want to re-generate the map, then just set the mapTable to nil.
Also you're better off leaving love.draw only to handle drawing graphics onto the screen, and put any generation or other things that need to be calculated per frame in love.update. For later stuff, if you want to use lua's builtin table functions later it's better to index everything starting from 1 as opposed to 0.
thank you for the reply. I know this is going to seem like a super basic question but can you explain the whole 'table' thing? I am mostly self taught in love2d and i have never used such a thing. i am not aware what the whole "mapTable" is. is it arrays? a few years ago i took an intro to c++ class but i remember very little. i will also try this code and see it works, thanks again
Okay I skimmed through the code again and it seemed to refresh my memory on arrays, correct me if I am wrong but the way I see it basically runs the for loop and stores the random values in the 9 different cells of the array and then references them again in order while drawing the images?
Yep, that's the gist of it. The terminology may be confusing, as what table and array are kinda depends on the code language in question. For example C/C++ has arrays but no "table" type as that term is usually reserved for 2D arrays. In lua everything is just a table given it's the sole container type variable you can have.