Good practices to get position of a Scene from multiple instances of another Scene.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JulienL

I’ve got a scene named Player and an other named Enemy.

Each 0.5s a new Enemy is instancied and goes toward the Player.
In the Enemy script, I have a reference to the Player with a get_node("../Player") and get the position from here but it makes my code brittle.

I’d like to know some alternatives. I tried to use signal but I can’t connect the Player to each new Instance. In my Main node, I have$Player.connect("has_moved", $Enemy, "_update_player_position") it connects only to the first instance of Enemy so it doesn’t work.

Do you have any suggestions to make it work ? Thanks !

:bust_in_silhouette: Reply From: Inces

It is best to use Autoload as absolute signal emitter. This is bread and butter of observer pattern. Introduce all signals in Autoload, so every node only has to connect himself to Autoload on their own ready() function. When You want a node to emit, instead make it call Autoload to emit.