How to create a 2d endless map generator?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By soul_gugu

I create 3 maps which will be appended at the end of current map randomly. Element structure of my level is:-

Node2d
|-Player
|-TileMap //initial map
|-Spawner(a 2d Node) // responsible to append levels in future

Following script is attched to spawners:-

extends Node2D
export(Array,PackedScene) var scenes

var random_scene=RandomNumberGenerator.new()
var selected_scene_index=0
func _on_Timer_timeout():
    random_scene.randomize()
    selected_scene_index=random_scene.randi_range(0,scenes.size()-1)
    var tmp=scenes[selected_scene_index].instance()
    add_child_below_node(self,tmp)

The code is working as expected,i.e., generating maps but how can i set the position of maps so that they appear after one another and do not overlap.

:bust_in_silhouette: Reply From: njamster

Like you set the position of everything else as well: change the global_position of your map scenes. Given your initial map is a TileMap, I’ll assume the map scenes are TileMap-nodes as well. You can use get_used_rect to get a TileMaps dimensions in tiles, multiply it with the tile-size (check the cell_size-property!) and offset it by the TIleMaps position (if it’s not Vector2.ZERO) and you’’ end up with global coordinates.