Connect signal to camera from programatically instantiated scene

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

I am programmatically instantiating a scene with the player character. When this scene is instantiated I want to send a signal to the camera. The signal should set the player character to be the target of the camera.

This is the main scene structure

GameManager
    Camera
    SceneSwitcher
        (instantiated scene will be here)

The new scene is instantiated as a child of the SceneSwitcher. This is SceneSwitcher script where I am trying to connect the signal from the instantiated scene.

func loadScene(scene):
    var newScene = load(scene).instance()
    add_child(newScene)
    newScene.connect("focus", camera, "setCameraTarget")

The instantiated scene is emitting this signal:

signal focus(target)

func _on_Player_tree_entered():
    emit_signal("focus", player)

This is the function in the camera that I want to run when the signal connects:

func setCameraTarget(object):
    print("camera set on: ", object.name)
    target = object

Seems like the signal never connects because I never see the print message “camera set on: player”.

:bust_in_silhouette: Reply From: rossunger

is _enter_tree() signal connected to the _on_Player_tree_entered?

did you try adding a print(“tree entered”) into the _on_Player_tree_entered() function to make sure it’s actually getting called?

It is, I did exactly that and the _on_Player_tree_entered() is getting called.

imalexandra | 2022-02-06 15:41