- Edited
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
# The array that holds the walkers x and y pos
var walker = {"x":0,"y":0}
# The array that holds the rooms positions
var rooms = []
# Holds the player scene instance
var player
# Boolean var that checks if the room is unique
var uniqueRoom
# The amount of rooms to spawn
var roomCount = 15
# When the code loads it will generate the dungeon.
func _ready():
generate_dungeon()
# Function to check if there is a unique room (THIS ISN'T WORKING')
func check_for_unique_room():
# Initially sets this to true
uniqueRoom = true
# Loops through all of the rooms in the array so far
for room in rooms:
# If the room in the array is equal to the room at the end of the array
# (This is when this function gets called. It is checking the new room against the other rooms)
if room.get_position() == rooms.back().get_position():
# Remove the room because it isn't unique
rooms.pop_back()
# Set unique to false
uniqueRoom = false
# Break out of the loop
break
# The function to actually generate the rooms and positions
func generate_dungeon():
# Keeps it random on load
randomize()
# This is where you choose how many rooms the dungeon spawns
while rooms.size() <= roomCount:
# If rooms is empty it means it is the first room. So if it isn't empty, do this code
if rooms.empty() == false:
# Initially sets the uniqueness of the room to false
var uniqueRoom = false
# While the room isn't unique, do the following code
while uniqueRoom == false:
# Add the room scene to the array
rooms.push_back(load("res://World/DungeonRoom.tscn").instance())
# Set the name of the room
rooms.back().set_name("room_" + str(rooms.size()) + "_" + str(walker["x"]) + '_' + str(walker["y"]))
# Calculate the position of the room
var room_pos = Vector2((335 * 1.5)*walker["x"], (195 * 1.5)*walker["y"])+Vector2(-335 * 1.5, -195 * 1.5)
# Set the position
rooms.back().set_position(room_pos)
# Adds the child to the current scene
self.add_child(rooms.back())
# Checks if the room is unique or not
check_for_unique_room()
# Makes the walker step
walker_step()
# If the room is unique do the following
if uniqueRoom == true:
# This sets the label. Just for testing purposes
rooms.back().get_node("Label").text = str(rooms.size())
# This shows the room which is originally hidden (Becuase of duplicate room issues I'm having)
rooms.back().show()
# Makes the walker step
walker_step()
# If the rooms array is empty it is the first room. Do this code
elif rooms.empty() == true:
# Push the room to the first array position
rooms.push_front(load("res://World/DungeonRoom.tscn").instance())
# Set the name of the room
rooms[0].set_name("room_0," + str(walker["x"]) + '_' + str(walker["y"]))
# Set the testing label room
rooms[0].get_node("Label").text = "0"
# Set the position of the room to 0, 0
rooms[0].set_position(Vector2.ZERO)
# Add the room to the scene
self.add_child(rooms[0])
# Show the room
rooms[0].show()
# If there is no player, add one to the room.
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()))
# Makes the walker step
walker_step()
else:
break;
# The function for the walker
func walker_step():
# The walker chooses a random number between 0 and 3. It will change the x or y postion
# depending on the number it chooses
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
# Just a function to generate a new dungeon for testing purposes
func _on_Button_pressed():
get_parent().remove_child(player)
get_tree().reload_current_scene()
Global.dungeonMusic = false