How to connect instanced scene's button signals to the main scene's root script?

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

I have Main scene, let’s call it Scene A. Then I have another scene, let’s call it Scene B. I instanced B in A. B (UI scene) has buttons. But when I want to connect the pressed signal to the A script, it’s not there. What’s the correct method to do it?

Thank you for your time!

:bust_in_silhouette: Reply From: Dead_lucky_32

You want to take the scene A and write the function you want to connect too

func _sceneB_connection:
          Pass

then when you instance the Scene B you want to connect that over to scene A within scene A

var bScene = sceneB.instance()
add_child(bScene)
bScene.connect("SIGNAL NAME", self, "_sceneB_connection" )

If you have any other questions, don’t be afraid to ask more questions!

FYI the signal name for button pressed is “pressed”

B script:

func _on_button_pressed():
    #how to transfer signal to the A script? (if it were in A script, I would directly write $object.rotate as "object" is in A script)

B scene instanced as a node in A scene.
A script:

var b_scene = get_node("scene_B").instance()

func _ready():
    add_child(b_scene)
    b_scene.connect ("on_button_pressed", self, "_on_button_pressed")

func _on_button_pressed():
    $object.rotate

Is it how that’s supposed to work? I kind of understand the concept but need some guidance to make it work.

Suleymanov | 2021-08-23 11:22

Oh, I get the logic now, in scene B I just need to keep it “pass”. Let me try it.

Suleymanov | 2021-08-23 11:29

UPDATE: I set everything as you mentioned except it returns error: Nonexistent function “instance” in base (my instanced scene name).

PS: I set:

var b_scene = get_node("scene_B").instance()

…above/before the func _ready():

Suleymanov | 2021-08-23 11:55

:bust_in_silhouette: Reply From: Suleymanov

PROBLEM SOLVED!