Advice about reparenting instantiated nodes and signals

Godot Version

4.7.2

Question

Hi all, as a Godot beginner, I am looking for advice about signals, reparenting, Curve2D and node structures in general.

At this point, I am working on a hotel manager system, where during the day guests arrive, the user assigns them to a room (My previous forum post) and in the morning they check out and the user earns money.

I instantiate a new guest and the node moves into its place in the queue. The user has an assign button on screen, which on click can calculate which the first free room is in the hotel.

Quick rundown of the problem I ran into:
For each room, there are PathFollow2D-s running to them. On clicking the assign button, the first guest in line needs to become the child of the corresponding PathFollow2D. Although I could probably find workarounds and alternative solutions to get the guests in the correct room visually, I fear that don’t yet understand the fundamentals of the mechanics of signals and reparenting instantiated nodes, making this seemingly simple system be more complicated than it needs to be.

If someone could help put me on the right path to figure this out, it would be greatly appreciated as I have struggled with this for days. If more specifics are needed for an answer I can share details.

You definitely need to be more specific. It’s not clear what you’re actually asking.

There’s no “instantiated nodes/scenes” as such. At runtime, there’s only one big scene tree of nodes. Once you instantiate a scene and add it to the scene tree it just becomes a branch of nodes in the scene tree. You can move/reparent branches of the scene tree any way you see fit. You can also connect signals between nodes in any way you like. Reparenting node branches won’t affect signal connections in any way. Those are two completely separated mechanisms.

I’ll try to explain.

var guest_scene = load("res://guest.tscn")

func _ready() -> void:
	var new_guest = guest_scene.instantiate()
	add_child(new_guest)

This action is what I mean by instantiating.

What I am trying to do is to reparent a specific new_guest after there has been multiple ones created. In theory, each one has a unique id attached to them from the Guest class, although :

class_name Guest
extends CharacterBody2D

static var id = 0
extends Guest

var guest_id
func _ready() -> void:
	guest_id = id
	id += 1

I hope this further explanation cleared up confusion, I didn’t realise I was so vague. Does this help you better understand my initial question? Thank you for taking your time to look over my problem.

You don’t need any id’s. If you have a reference to the top node of the instantiated scene, you can just reparent that node and its whole branch will follow.

That sounds like it could be my solution! Do you know where I can learn more about this? How would I reference nodes from an instantiated scene?

What’s returned by instantiate() is in fact a reference to the top instantiated node. You can store that somewhere for later use.

How do you discern between guests for other purposes?

I have been relying on the ID to store them in a dictionary like so: {RoomID : GuestID}.

Although, this is the first time I actually had to reference them separately, in the future I might need to come up with something more applicable.

I will now try to implement your advice, I hope I can get something to work :slight_smile: Thank you!

My final solution is the following:

extends Node2D

var guest_scene = load("res://guest.tscn")
var queue_list : Array

func _ready() -> void:
	var new_guest = guest_scene.instantiate()
	add_child(new_guest)
	queue_list.append(new_guest)

func _on_button_pressed() -> void:
	if queue_list.size() > 0:
		queue_list[0].assigned = true
		queue_list[0].reparent($"Path2D/PathFollow2D") # Hardcoded for testing purposes
		queue_list[0].position = Vector2(0,0) # Important so it actually follows the progress of the PathFollow2D
		queue_list.remove_at(0)

Turns out I have been overcomplicating this whole operation. Thank you @normalized for your advice, this simple method has not occurred to me.