Signal emiited but not caught/handled?

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

Hi there,

I am trying to emit a signal to my Camera but it is not being received by it.

My scene looks like this in terms of hierarchy:

  • RootNode
    • CameraParentNode
      • Camera
  • NodeWithScript

Camera has a simple script attached, which has a signal and a function that is triggered when this signal is received (this is configured via Node window in editor):

extends Camera

signal rotate_camera(direction)

func _on_Camera_rotate_camera(direction):
print(“Rotating camera!”)

NodeWithScripthas a script attached that emits under certain conditions:

emit_signal(‘rotate_camera’, Vector2(-sign(direction.x), 0.0))
print(“Emmited horizontal swipe”)

In runtime, I see “Emmited horizontal swipe” printed but not “Rotating camera!”.
What could be the reason for the emmited signal to not be received by the camera script?

:bust_in_silhouette: Reply From: AlexTheRegent

Signal should be defined in script that emit it. So your NodeWithScript script should have next lines:

signal rotate_camera(direction)
emit_signal('rotate_camera', Vector2(-sign(direction.x), 0.0))
print("Emmited horizontal swipe")

And node that want to receive signal should connect to it. Your Camera should have next lines:

func _ready():
    # connect node to signal
    get_node("/root/NodeWithScript").connect("rotate_camera", self, "on_Camera_rotate_camera")

func on_Camera_rotate_camera(direction):
    print("Rotating camera!")