Hey, so i’m working on a game in which players will have procedurally generated maps. it works great until the maps get large, but the maps feel too small otherwise.
function Generation.generateHeightmap(frequency, seed, size)
local heightmap = {}
for x=1,size do
local hx = {}
heightmap[x] = hx
for z = 1, size do
print("W")
local y = math.noise((x) / (frequency), (z) / (frequency), seed)
hx[z] = math.clamp(y + 0.5, 0, 1)
end
end
return heightmap
end
this bit of code generates a heightmap, but it is horribly slow. it leads to the script exhausting its exeecution time (whatever that means). i tried running it in a coroutine, but it is still having this issue. how can i optimise it?
You don’t give your loop a chance to yield, so it’s gonna try and do everything all at once, which is why your script is exhausting. Throw in a few task.wait() 's and you should be fine.
Well I don’t know how much this will improve your situation but here are a few tips.
When making tables you can do table.create(size, 1) This allocates the memory beforehand so you don’t have to reallocate every time you set a value.
Try not to print because printing a lot is not good for performance
Maybe you can make it so that you don’t calculate every height at once.
So you could add a heartbeat length wait every, I don’t know, 100 calculations.