I’m making a game, similar to terraria and I’m working on the world generation but it takes about 30 minutes to generate a 1000x500 world. I did some testing and found out that it was something with the less than calculations that decide what tile should be placed but I don’t know how I can fix it. Any help would be appreciated!
func generate_block(x: float, y: float, base_noise_val: float):
var cave_noise_val: float = cave_noise.get_noise_2d(x, y)
var base_val: float = y / 200
if base_noise_val < base_val - 0.2:
positions[3].append(Vector2i(x, y))
elif base_noise_val < base_val - 0.15:
positions[2].append(Vector2i(x, y))
elif base_noise_val < base_val - 0.3:
positions[4].append(Vector2i(x, y))
if cave_noise_val < (base_val / 2.5) - 0.25:
positions[0].append(Vector2i(x, y))
func set_cells():
for i in range(len(positions)):
set_cells_terrain_connect(0, positions[i], 0, i - 1)
instead of placing every tile at once, you should save the tiles for each different type in their own array and call set_cell_terrain_connect once with this array, so it doesnt have to recalculate old tiles. And if you want to reduce lag you can run it in a background thread
Thanks, I tried this and it does have a noticeable impact, but it seems like it’s the less than calculation that takes most of the performance. If I switch it to == it runs a lot faster, but it obviously doesn’t work correctly.
Actually, it now seems like the problem is the set_cells_terrain_connect function. If I comment out that function, it runs perfectly. In the code, there is an array of arrays var positions := [[], [], [], [], []] which contains arrays of position coordinates, for different block types, 0 is air, 1 is grass, 2 is stone, etc. Then, in the generate_block function, it appends the x and y to a certain array in positions (e.g. positions[0].append(Vector2i(x, y))), and then the custom set_cells function runs set_cells_terrain_connect for every item in the positions array. The huge set_cells_terrain_connect function seems to be the bottleneck, but I can’t think of any good solution.
It is a calculation heavy method and i dont think you can do something about it.
But as @zoeren said you should statically-type your variable this improves performance