Getting a signal from object notifications possible?

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

I am building an add-on that requires the editor’s camera. Using position every frame made the result I want look laggy. So I figured that I could use a signal that activates every time the camera’s transform changes. There is a notification called NOTIFICATION_TRANSFORM_CHANGED. I thought could somehow use notifications with signals. But how?

:bust_in_silhouette: Reply From: wyattb

Have you tried observing your camera transform via connect? E.g.

func _cam_transform(data):
     ... do something ..
      pass

func _ready():
	var res=$MyCamera.connect("NOTIFICATION_TRANSFORM_CHANGED", self, "_cam_transform",["Cam Transform data"])

Nonexistent signal: NOTIFICATION_TRANSFORM_CHANGED

SIsilicon | 2018-11-08 17:21

:bust_in_silhouette: Reply From: elacim

You can use the _notification() function present in both Godot 3 & 4 and a few other steps to achieve what you want.

I believe you have to use set_notify_transform(enable: bool) with enable set to true to be able to receive the NOTIFICATION_TRANSFORM_CHANGED notification.

Here’s the code that I found that works to do this:

signal camera_has_transformed(new_position)

func _ready():
set_notify_transform(true)

func _notification(what: int):
if what == NOTIFICATION_TRANSFORM_CHANGED:
emit_signal(“camera_has_transformed”, camera.global_position)

Apologies for being 5 years late, but I hope this helps anyone else in the future. Here’s some more documentation regarding it if you need it (which is where I just learnt this from) . This code should work regardless of using Godot 3/4.