Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Rochambeau |
Hi,
I’m working on a 2D Zelda-like game. The map is procedurally generated.
for x in range(chunk_size):
for y in range (chunk_size):
var a = terrain_noise.get_noise_2d(x+x_pos,y+y_pos)
if a > snow_height:
terrain_tile = SNOW
elif a > mountain_height:
terrain_tile = MOUNTAIN_DARK
elif a > lower_mountain_height:
terrain_tile = MOUNTAIN_LIGHT
elif a > grass_height:
terrain_tile = GRASS[randi()%GRASS.size()]
elif a > sea_level:
terrain_tile = SAND
elif a > deep_sea_level:
terrain_tile = SHALLOW_WATER
else:
terrain_tile = DEEP_WATER
$Navigation2D/TerrainMap.set_cell(x+x_pos,y+y_pos,terrain_tile)
spawn_guard(x_pos,y_pos)
All tiles in the tileset have a square navigation poligon of the exact tile size.
After the map is generated, some NPC should spawn a get to move around:
func spawn_guard(x_pos,y_pos):
yield(get_tree().create_timer(1.0), "timeout")
var spawn = guard.instance()
var posi = Vector2(get_parent().get_node("World/Navigation2D/TerrainMap").map_to_world(Vector2(x_pos+20,y_pos+20)))
spawn.position = posi
var goal = Vector2(get_parent().get_node("World/Navigation2D/TerrainMap").map_to_world(Globales.player_pos))
spawn.goal = goal
var path = $Navigation2D.get_simple_path(posi,goal)
spawn.path = path
add_child(spawn)
My problem is:
when yield is called at the beginning of the spawn_guard function to have a pause between map generation and spawning, everything works fine.
When I remove the yield line, the variable path is empty.
It looks like the map is not yet “finally built” (navigation poligons connected?), so get_simple_path returns an empty array.
How can I make sure, the tilemap is completed, so get_simple_path has all navigation poligons to get to work?