I am relativly new to coding, especially GDScript. However, I was planning on making a game that requires a maze generator. I found a tutourial online that sets up a Node2D and inserts the following code:

``````#My Maze Generator
extends Node2D

var width=19; var height=width; var start= Vector2(1,1); var goal= Vector2(width-2,height-2)
var queue=[start]; var cell; var first_cell; var i; var j; var neighbor_cell; var path
var grid=[]; var x1=0; var y1 = 0; var x2=0; var y2=0; var size_cell

size_cell = get_viewport_rect().size/width
for i in range(0,width): grid.append([])
for j in range(0,height): grid[i].append(-1); pass

func carve_maze(x,y):
var dir = int(rand_range(0,3)); var count=0
while count < 4:
var dx = 0; var dy = 0;
if dir==0: dx=1
elif dir==1: dy=1
elif dir==2: dx=-1
else: dy = -1
x1=x+dx; y1=y+dy; x2=x1+dx; y2=y1+dy
if x2>0 and x2< width and y2>0 and y2<height:
if grid[x1][y1]== -1 and grid[x2][y2]==-1:
grid[x1][y1]=-2; grid[x2][y2]=-2; carve_maze(x2,y2)
count=count+1
dir=(dir+1)%4

func generate_maze():
randomize()
grid[1][1]=-2; carve_maze(1,1)
return grid

func mark_cell(x,y,cell):
if (0 <= x<height and 0 <= y < width and grid[x][y]==-2):
grid[x][y]=cell+1
queue.append(Vector2(x,y))

func solve_grid(grid, start, goal):
grid[start.x][start.y]=0
while not (queue.empty()):
first_cell=queue[0]
queue.pop_front()
cell=grid[first_cell.x][first_cell.y]
mark_cell(first_cell.x-1,first_cell.y,cell)
mark_cell(first_cell.x+1,first_cell.y,cell)
mark_cell(first_cell.x,first_cell.y-1,cell)
mark_cell(first_cell.x,first_cell.y+1,cell)
if grid[goal.x][goal.y]>0:return cell+1
return -1

func move(cell):
for neighbor_cell in [Vector2(i+1,j), Vector2(i-1,j), Vector2(i,j+1), Vector2(i,j-1)]:
if (0<=neighbor_cell.x < height and 0 <= neighbor_cell.y < width and grid[neighbor_cell.x][neighbor_cell.y] ==cell):
i=neighbor_cell.x; j=neighbor_cell.y
neighbor_cell=Vector2(neighbor_cell.x,neighbor_cell.y)
return neighbor_cell

func findPath(grid,goal):
i=goal.x; j=goal.y
cell=grid[i][j]
path = [Vector(i,j)]
while cell > 0:
cell -= 1
neighbor_cell=move(cell)
path=[neighbor_cell]+path
return path

func _draw():
generate_maze()
solve_grid(grid,start,goal)
findPath(grid,goal)
grid[1][0] = -2; grid[width-2][height-1]=-2
for y in range (0,grid.size()):
for x in range(0,grid.size()):
if grid[y][x]==-1: draw_rect(Rect2(x*size_cell.y,y*size_cell.y,size_cell.y-2,size_cell.y-2), Color(0.5,0.3,0.3))
else: draw_rect(Rect2(x*size_cell.y,y*size_cell.y,size_cell.y-2,size_cell.y-2),Color(0,1,0))
for find in path: draw_rect(Rect2(find.y*size_cell.y,find.x*size_cell.y, size_cell.y-2,size_cell.y-2), Color(0,1,0))
``````

Yet, when I start the project just freezes. Any clues on how to fix this?

in Engine
edited

"just freezes" - no error message?

Also, it would be much easier to understand your code if your formatting wasn't messed up. You can use the code button above (looks like {}) or just put a 4-space indent in front of each line:

``````func _ready():
pass
``````

Fixed the formatting.

I have not looked at the code but I recommend you to put a breakpoint and follow it, maybe you have made an infinite loop somewhere.

If the Maze is very large, the findPath() could very well just kill the application.