0 votes

Hey guys! So, I'm working on a project and am looking to build a binding of isaac type of dungeon generation. I've build it out, but keep getting issues with it. I wanted it to loop through and have exactly the amount of rooms that I specify, but when I added a unique room check it just keeps crashing.

Could you point to me what I'm doing wrong?

I know my code is probably garbage, and I'm sure the answer is obvious.

extends Node

var walker = {"x":0,"y":0}
var rooms = []
var player
var uniqueRoom


func _ready():
    generate_dungeon()

func check_for_unique_room():
    uniqueRoom = true
    print(rooms)
    for room in rooms:
        print(str(room.get_position()) + " --- " + str(rooms.back().get_position()))
        if room.get_position() == rooms.back().get_position():
            print(str(room.get_position()) + " --- " + str(rooms.back().get_position()))
            rooms.pop_back()
            uniqueRoom = false
            break
        if uniqueRoom == true:
            break


func generate_dungeon():
    randomize()

    while rooms.size() <= 15:

        if rooms.empty() == false:

            print("not first room")
            var uniqueRoom = false

            while uniqueRoom == false:
                rooms.push_back(load("res://World/DungeonRoom.tscn").instance())
                rooms.back().set_name("room_" + str(rooms.size()) + "_" + str(walker["x"]) + '_' + str(walker["y"]))
                var room_pos = Vector2((335 * 1.5)*walker["x"], (195 * 1.5)*walker["y"])+Vector2(-335 * 1.5, -195 * 1.5)
                rooms.back().set_position(room_pos)
                self.add_child(rooms.back())
                check_for_unique_room()
                print('')
                walker_step()

            if uniqueRoom == true:
                rooms.back().get_node("Label").text = str(rooms.size())
                rooms.back().show()
                print(rooms.size())
                print('Room ' + str(rooms.size()) + ' = ' + str(rooms.back().get_position()) + " " + str(walker))
                walker_step()




        elif rooms.empty() == true:
            rooms.push_front(load("res://World/DungeonRoom.tscn").instance())
            rooms[0].set_name("room_0," +  str(walker["x"]) + '_' + str(walker["y"]))
            rooms[0].get_node("Label").text = "0" 
            rooms[0].set_position(Vector2.ZERO)
            self.add_child(rooms[0])
            rooms[0].show()
            print('Room 0 = ' + str(rooms[0].get_position()) + " " + str(walker))
            if get_tree().get_root().has_node("Player") == false:
                player = load("res://Player/Player.tscn").instance()
                player.set_position(rooms[0].get_position() + Vector2((320 /2), (180/2)))
                player.show()
                get_parent().call_deferred("add_child", player)
                print("Player = " + str(player.get_position()))

            print(rooms.size())
            print('Room ' + str(rooms[0]) + ' = ' + str(rooms[0].get_position()) + " " + str(walker))
            walker_step()               
            print('first room')
        else:
            break;
    for room in rooms:
        print(room.get_name())

func walker_step():
    var choice = (randi() % 4)
    if choice == 0:
        walker["x"]+=1
    if choice == 1:
        walker["x"]-=1
    if choice == 2:
        walker["y"]+=1
    if choice == 3:
        walker["y"]-=1
asked Jun 19 in Projects by Roodiger (14 points)

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.