What's the best way to communicate between sibling nodes?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BFGames
:warning: Old Version Published before Godot 3 was released.

I’m working on a project in 2.1.4, which is mostly just GUI interfaces and switching between scenes at the moment. Scene tree looks like this:

  • main (Node)
    – instanced scene 1 (Control)
    – instanced scene 2 (Control)

… and so on (instanced in the editor rather than code btw). Now if I want a button press in scene 1 to activate a function in scene 2, what is the best way to go?

I’ve tried signals, but I’ve spent three evenings trying various approaches to this (signal manager singleton, get_tree().get_root().get_node()), but the latter always seems to return a null instance, and the former is even more complicated. Of course, I’ve tried to link by creating the signal and linking through the editor signal window too, but that doesn’t produce anything!

Has anyone got any advice to achieve something like this? Any help would be much appreciated.

EDIT: The problem was that I was using get_tree().change_scene(), which was reinstancing a scene on its own, when I wanted it to just switch between them. I’ve since swapped to hiding and showing the various scenes, and signals are now working using the ("../scene1") etc., which I know is bad practice, but i’ll come back and tidy up once i’ve finished a prototype.

Are you looking to communicate between scenes? I did a quick search and found this.

davidpgil | 2017-10-26 23:55

Yeah, I’ve previously made that game and rewatched when I hit this problem, but actually it looks like I need to re-look at how i’m switching between scenes and make sure i’m not de-instancing scenes as I go (see below response …)

Thanks for the response!

BFGames | 2017-10-27 09:48

:bust_in_silhouette: Reply From: eons

First, be sure these siblings are not both part of the same scene, if they have independent behaviour, is most likely they are unique, but if have a constant communication between siblings, both may be part of the same scene (meaning that you are exceeding the atomization).

Then, signals, yes! but when?
Your manager get nulls probably because is trying to make connections of things that are not yet in the tree, there is where your main scene node need to work, when the current scene root (not the tree root) is ready, means that all it’s children are ready, and that is the moment where you can “wake up” your signal manager to get and connect nodes.

Also, try to group these nodes so they are easier to get from anywhere on the tree with get_nodes_in_group (and the current scene root does not need to know where are they).

Yup, i’m an idiot - I worked it out just before I went to bed last night - the scenes are all instanced but I switch between the different instanced scenes with get_tree().change_scene() - not realising that it was instancing a new scene that didn’t have the siblings.

Now I know the problem i’ll fix it, retry the methods I was using and post what worked for me. The group thing is interesting though and I’ll look into that too.

Thanks for the help.

BFGames | 2017-10-27 09:42