0 votes

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

func _ready():
    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)

func generate_maze():
    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):

func solve_grid(grid, start, goal):
    while not (queue.empty()):
        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
            return neighbor_cell

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

func _draw():
    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 by (20 points)
edited by

"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():

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.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.