0

I'm making a system that spawns a scene with a premade room, even though the TileMap nodes contain the exact same set, they don't interact with other giving this weird sectioned feeling to them. I attempted to make a code to get them fuse into one node but it seems I may be confusing myself on how to use local/global coordinates of the cells.

Sectioned rooms image:

1

My idea is ask the room scene for the cells using a ID with the method get_used_cells_by_id and then using set_cell to add them to the TileMap in the main scene. Any help is appreciated

func updateDungeonTileMap():
    var maintileMap = $TileMap
    var tileMapID:Array = [0, 1, 2]
    var roomCheck:Array = $Rooms.get_children()
    for i in tileMapID.size():
        for o in roomCheck.size():
            var roomTileMap = roomCheck[o].getTileMap()
            var cells:Array = roomCheck[o].getTileMapCells(i)
            for u in cells.size():
                var globalCord:Vector2 = roomTileMap.to_global(cells[u])
                var localCord:Vector2 = maintileMap.to_local(globalCord)
                maintileMap.set_cell(localCord[0], localCord[1], i)
    for i in roomCheck.size():
        roomCheck[i].removeTileMap()
    maintileMap.update_bitmask_region()

func getTileMapCells(id):
    return $TileMap.get_used_cells_by_id(id)
Francesco - FL
  • 603
  • 1
  • 4
  • 25
Sacul
  • 11
  • 3

1 Answers1

1

After testing and checking the numbers for a couple of hours I found out the problem was that I wasn't multiplying the global coordinates by my tile size:

var globalCord:Vector2 = roomTileMap.to_global(cells[u]*16)

func updateDungeonTileMap():
    var maintileMap = $TileMap
    var tileMapID:Array = [0, 1, 2]
    var roomCheck:Array = $Rooms.get_children()
    for i in tileMapID.size():
        for o in roomCheck.size():
            var roomTileMap = roomCheck[o].getTileMap()
            var cells:Array = roomCheck[o].getTileMapCells(i)
            for u in cells.size():
                var globalCord:Vector2 = roomTileMap.to_global(cells[u]*16)
                var localCord:Vector2 = maintileMap.world_to_map(globalCord)
                maintileMap.set_cell(localCord[0], localCord[1], i)
    for i in roomCheck.size():
        roomCheck[i].removeTileMap()
    maintileMap.update_bitmask_region()
Sacul
  • 11
  • 3
  • I see. Well, the function `map_to_world` will do that multiplication. Then again, `world_to_map` takes local coordinates, not global. – Theraot Sep 06 '21 at 03:28