# How to convert this code from using TileMaps into using Scenes

Attention Topic was automatically imported from the old Question2Answer platform.

I’m trying to make a Binding of Isaac clone and I’m stuck on room generation.
I’m trying to convert the following code below:

``````const N = 1
const E = 2
const S = 4
const W = 8

var width = 3  # width of map (in tiles)
var height = 3  # height of map (in tiles)

func make_maze():
for x in range(width):
for y in range(height):
Map.set_cellv(Vector2(x, y), N|E|S|W)
``````

Into something the will work with scenes instead of TileMaps. I’ve already got this:

``````const N = 1
const E = 2
const S = 4
const W = 8

var width = 3  # width of map (in tiles)
var height = 3  # height of map (in tiles)

func make_maze(tile):
for x in range(width):
for y in range(height):
var lvl = tile.instance()
lvl.position = Vector2(x,y)
``````

This works for adding one scene, but not for randomly generating the next scenes. Any help would be appreciated. I feel like I’m in over my head with this but I don’t know where to start.

I was able to make this work kinda by using a match statement and export (PackedScene) :

``````extends Node2D

const N = 1
const E = 2
const S = 4
const W = 8

var cell_walls = {Vector2(0, -1): N, Vector2(1, 0): E,
Vector2(0, 1): S, Vector2(-1, 0): W}

var tile_size = 224  # tile size (in pixels)
var width = 4  # width of map (in tiles)
var height = 4  # height of map (in tiles)

# get a reference to the map for convenience

export (PackedScene) var lvel15
export (PackedScene) var lvel14
export (PackedScene) var lvel13
export (PackedScene) var lvel12
export (PackedScene) var lvel11
export (PackedScene) var lvel10
export (PackedScene) var lvel09
export (PackedScene) var lvel08
export (PackedScene) var lvel07
export (PackedScene) var lvel06
export (PackedScene) var lvel05
export (PackedScene) var lvel04
export (PackedScene) var lvel03
export (PackedScene) var lvel02
export (PackedScene) var lvel01
export (PackedScene) var lvel00

randomize()
\$Player.position = Vector2(224*1.5,224*1.5)
tile_size = Map.cell_size
make_maze()

func post_import(lvl, pos):
var LVL = lvl.instance()
LVL.position = pos

func check_neighbors(cell, unvisited):
# returns an array of cell's unvisited neighbors
var list = []
for n in cell_walls.keys():
if cell + n in unvisited:
list.append(cell + n)
return list

func make_maze():
var unvisited = []  # array of unvisited tiles
var stack = []
# fill the map with solid tiles
Map.clear()
for x in range(width):
for y in range(height):
unvisited.append(Vector2(x, y))
Map.set_cellv(Vector2(x, y), N|E|S|W)
var vectorSize = Vector2(x*tile_size.x,y*tile_size.y)

match 15:
15:
post_import(lvel15,vectorSize)

var current = Vector2(0, 0)
unvisited.erase(current)
# execute recursive backtracker algorithm
while unvisited:
var neighbors = check_neighbors(current, unvisited)
if neighbors.size() > 0:
var next = neighbors[randi() % neighbors.size()]
stack.append(current)
# remove walls from *both* cells
var dir = next - current
var current_walls = Map.get_cellv(current) - cell_walls[dir]
var next_walls = Map.get_cellv(next) - cell_walls[-dir]
Map.set_cellv(current, current_walls)

var currents = current*224
match current_walls:
0:
post_import(lvel00,currents)
1:
post_import(lvel01,currents)
2:
post_import(lvel02,currents)
3:
post_import(lvel03,currents)
4:
post_import(lvel04,currents)
5:
post_import(lvel05,currents)
6:
post_import(lvel06,currents)
7:
post_import(lvel07,currents)
8:
post_import(lvel08,currents)
9:
post_import(lvel09,currents)
10:
post_import(lvel10,currents)
11:
post_import(lvel11,currents)
12:
post_import(lvel12,currents)
13:
post_import(lvel13,currents)
14:
post_import(lvel14,currents)
15:
post_import(lvel15,currents)

Map.set_cellv(next, next_walls)
current = next
unvisited.erase(current)
elif stack:
current = stack.pop_back()

yield(get_tree(), 'idle_frame')
``````

The problem happens with any dead ends. They don’t generate, but if I do the same match statement with the next line of code I get overlapping scenes which block entrances/exits.
Using a count to only use the match statement on the last round works if there is only one dead end, but doesn’t work if there are 2 or more.
Ugh, I’m one step closer but two steps back with this solution.