General question about signals for enemies shooting bullets

Godot Version

4.2.1

Question

I have a Main node that carries a bunch of instanced nodes. Some of them are enemies already in the scene tree. Some enemies will be instanced by other enemies, so they’re not in the editor. The Main node itself is a child of level 1, level 2, etc. so I can change levels.

How do I connect a signal between an enemy and the Main node, whether the enemy is spawned in code or manually added in the scene tree? I don’t want to reference an absolute path in the enemy’s _ready() to the Main node because the main scene path might change.

This is currently how I’m connecting my player node to the Main node:

func _ready():
	connectSignals()

func connectSignals():
	var players
	players = get_tree().get_nodes_in_group("player")
	for i in players:
		i.spawnBullet.connect(_on_player_bulletSpawn)

I could do something similar for enemies, but that wouldn’t count enemies that are instanced later.

if you add enemies programmatically, you can connect them before they are added as children.

The second option is have a singleton that sets up the connection, via the enemies _ready function.