okay so i managed to find a way to do what i had in mind :
what i needed was to be able to spawn multiple time the same room, the issue i had was that i couldn’t do that with .instantiate()
because it only works on PackedScene.
i also thought that i couldn’t use PackedScenes in a convenient way because i thought i would have to declare PackedScenes with load and then assign them manually to their groups to tell the game where they can and can’t spawn since using is_in_group()
doesn’t work on a PackedScene.
however i found on this thread : Accessing some (but not all) information from a PackedScene - #2 by a52
that PackedScenes have a method called get_state()
get_state()
allows you to see info about a PackedScene that you wouldn’t be able to see otherwise.
in my case i was able to see the groups of the PackedScenes under the form of a PackedStringArray and assign the scenes depending on the values in the PackedStringArray :
func sort():
for scene in sorting_list:
var scene_groups = scene.get_state().get_node_groups(0)
if scene_groups.has("Bottom") == true:
bottom_rooms.append(scene)
if scene_groups.has("Top") == true:
top_rooms.append(scene)
if scene_groups.has("Left") == true :
left_rooms.append(scene)
if scene_groups.has("Right") == true:
right_rooms.append(scene)
if scene_groups.has("Starting_Room") == true:
starting_rooms.append(scene)
so here i made a for loop that runs for as many scenes as there is in sorting_list (i had to put all my rooms in my sorting_list manually because i don’t have any idea on how to do it via code)
then i create the var scene_groups which gets from the scene a PackedStringArray that contains the groups that the PackedScene has.
at first i thought that PackedStringArrays worked just like arrays but not really so you can’t use the methods you would otherwise
thanks to the godot documentation that you get when pressing ctrl click i found the methods .find()
and .has()
for PackedStringArrays
I tried using .find()
first but it didn’t work quite as i hoped it would.
however, .has()
was just what i needed, it checks the packedStringArray and sees for each value if it corresponds to what you put in the () of the method.
anyways here’s the full code (unfinished) code for anyone intrested :
extends Node2D
var top_rooms = []
var bottom_rooms = []
var left_rooms = []
var right_rooms = []
var starting_rooms = []
@onready var Vcorridor : PackedScene = load("res://Rooms/Stage 1/Vcorridor.tscn")
@onready var Hcorridor : PackedScene = load("res://Rooms/Stage 1/Hcorridor.tscn")
@onready var Start_Room1 : PackedScene = load("res://Rooms/Stage 1/start_room_1.tscn")
@onready var Start_Room2 : PackedScene = load("res://Rooms/Stage 1/start_room2.tscn")
@onready var BDE1 : PackedScene = load("res://Rooms/Stage 1/bde_1.tscn")
@onready var start_room_spawned : bool = false
@onready var rng = RandomNumberGenerator.new()
@onready var starting_room
@onready var current_child_number = 0
@onready var budget = 5
@onready var sorting_list = [Vcorridor,Hcorridor,Start_Room1,Start_Room2,BDE1]
func _ready():
Clear()
sort()
Spawn_Rooms(null)
func sort():
for scene in sorting_list:
var scene_groups = scene.get_state().get_node_groups(0)
if scene_groups.has("Bottom") == true:
bottom_rooms.append(scene)
if scene_groups.has("Top") == true:
top_rooms.append(scene)
if scene_groups.has("Left") == true :
left_rooms.append(scene)
if scene_groups.has("Right") == true:
right_rooms.append(scene)
if scene_groups.has("Starting_Room") == true:
starting_rooms.append(scene)
func Clear():
remove_child($Start_room2)
remove_child($Start_Room1)
remove_child($BDE1)
remove_child($Hcorridor)
remove_child($Vcorridor)
func Spawn_Rooms(Current_Room):
if start_room_spawned == false: #Looks if a starting room was created
var random_room = rng.randi_range(0,starting_rooms.size()-1)
var room_instance = (starting_rooms[random_room]).instantiate()
add_child(room_instance) # picks a random room in the starting rooms array
start_room_spawned = true
Spawn_Rooms(room_instance)
else:
while budget > 0 :
if is_instance_valid(Current_Room.get_child(1)) == true : #gets the first mark of the current room
if Current_Room.get_child(1).is_in_group("Top_Mark") :
var room_instance = top_rooms.pick_random().instantiate()
Current_Room.get_child(1).add_child(room_instance) #spawns the room from the instance
var child = Current_Room.get_child(1).get_child(0) #gets the room that was just spawned
print(child)
budget -= child.Budget_Cost
while child.get_child(current_child_number).is_in_group("Bottom_Mark") == false :
current_child_number+=1
var mark_pos = child.get_child(current_child_number).position
mark_pos = mark_pos*-1
child.position = mark_pos
Spawn_Rooms(child)
elif Current_Room.get_child(1).is_in_group("Left_Mark") :
pass
elif Current_Room.get_child(1).is_in_group("Right_Mark") :
pass
right now all it does is spawn corridors on the top side of the starting room until the budget runs out but i solved my problem of not being able to create multiple time the same room (was mostly a lack of knowledge on PackedScenes to be honest)