#map generation

1 messages · Page 1 of 1 (latest)

sullen crown
#

generate in another thread

proven crest
#

there will be empty places and sometimes the game crash

buoyant ivy
#

What do u mean can’t place the thread?

#

Offloading the work to another thread and maybe making the chunks smaller are the only real options you got i think

proven crest
#

i made the thread place the areas but look what i get

#

even in small chunks it make them (20x20)

#

when i use the main thread to place everything in it place but it need to freeze for at least 20ms

buoyant ivy
#

Maybe share ur code? Its weird that it would stutter for such small operations, we would need some more details

proven crest
#
func create_map()->void:
    noiseHeight_texture = WorldManager.Globalnoise
    noise = noiseHeight_texture.noise
    _start_generation()

func _start_generation() -> void:
    var thread:Thread = Thread.new()
    thread.start(_generate_on_thread)

func _generate_on_thread() -> void:
    var start_x := WorldManager.playerMapNumber.x * WorldManager.map_size - WorldManager.map_size
    var start_y := WorldManager.playerMapNumber.y * WorldManager.map_size - WorldManager.map_size
    var end_x   := start_x + WorldManager.map_size
    var end_y   := start_y + WorldManager.map_size
    _water_array.clear()
    _sand_array.clear()
    _dirt_array.clear()
    _grass_array.clear()
    _object_array.clear()
    for x in range(start_x, end_x):
        for y in range(start_y, end_y):
            var n: float = noise.get_noise_2d(x, y)
            var cell := Vector2i(x,y)
            
            if n >= 0.1 :
                _grass_array.append(cell)
            if n >= 0 and n <= 0.2 :
                _dirt_array.append(cell)
            if n >= -0.2 and n <= 0.1:
                _sand_array.append(cell)
            if n <= -0.2 :
                _water_array.append(cell)
    
    print("water array :",len(_water_array))
    call_deferred("_apply_tiles")
    print("thread done")

func _apply_tiles() -> void:
    var start := Time.get_ticks_msec()
    
    for cell in _water_array:
        water_layer.set_cell(cell, SOURCE_WATER, Vector2i(0, 0))
    land_layer.set_cells_terrain_connect(_sand_array,  TERRAIN_SAND, 0)
    land_layer.set_cells_terrain_connect(_dirt_array,  TERRAIN_DIRT, 0)
    land_layer.set_cells_terrain_connect(_grass_array, TERRAIN_GRASS, 0)
    
    
    var end := Time.get_ticks_msec()
    print("create_map took: %d ms" % (end - start))    
#

here is the code thata generate the world

#

well chunks i did shrink the size to make it match

#

for now i'm using a button to make the world but it freezes a little like i said

#

if you the action of the button is what makes it freeze i was planning on make it automatic tmrw

buoyant ivy
#

There a few problems i think, i will give a detailed response in a few hours if i am behind my pc, if no one else has by that time

proven crest
#

okayy htanks in advance *

buoyant ivy
#

So, am not gonna lie the part where you calculate the grid where you want to place new tiles is confusing me and I hope it is just a chunk of 20 tiles insteald of recalculation everything but if it is, then you are doing double operations on :

if n >= 0.1 :
                _grass_array.append(cell)
            if n >= 0 and n <= 0.2 :

And

if n >= -0.2 and n <= 0.1:
                _sand_array.append(cell)
            if n <= -0.2 :
                _water_array.append(cell)

if it's -0.2, even though this isn't that costly, in terms of optimization it doesn't make sense.

In the end the biggest cost will come from apply_tiles, where you are placing tiles, this is still being done on the main thread

proven crest
#

you mean the overlaying

#

no i did that on purpose

#

so the layers become puch smoother

#

bc without it everything will seems unconnected