How to find out which node emitted a signal?

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

I have a pause panel in my game, in which there are sliders to change the volume of different nodes, brightness of screen, etc. I plan to connect the on_slider_value_changed signal to make the effects of the slider apply on the corresponding node. Currently, I use different signals for every slider

My question is, how do I find out which node emitted the signal?

The connections work properly as it is, but there’s a good bit of code which might be redundant if I could find out which node emitted the signal.
I can then use a match() function, eliminating a lot of different signal connections in code.

:bust_in_silhouette: Reply From: clemens.tolboom

Using the connect() method you can add extra information in last array argument:

Error connect(signal: String, target: Object, method: String, binds: Array = [ ], flags: int = 0)

  • Create a new 2D Scene
  • Add a HSlider and a Node2D to this scene
  • Attach code to your scene
  • Add code from below
  • Run the scene
  • Change the slider

Code example

extends Node2D

onready var slider:HSlider = $HSlider
onready var my_node = $Node2D

func _ready():
    var _d = slider.connect("value_changed", self, 'slider_changed',[my_node])

func slider_changed(value, node):
    print_debug(value, ' ', node)

The signal being emitted is a pre-existing signal in the engine

How do I add/set the value of the user defined parameter in the connect() function for each signal?

For example:

  1. Slider for music volume
  2. Slider for SFX volume

When I alter the 1st slider, it automatically emits a slider value changed () signa
There’s no place in the code where I can set a parameter, because I don’t know when it is emitted

strive_and_conjure | 2021-01-22 10:41

It’s hard to answer without your code in your question. I’ll update my answer and hope that helps

clemens.tolboom | 2021-01-22 12:56

Oh, of course! I didn’t think of that. Thank you, that helps enough.

So basically I was writing the connect() function at the receiving end of the signal, and hence I had no way of actually accessing the Hslider or node2d (in my case there are multiple sliders emitting the same signal, so I can’t know where the signal is coming from). But if I simply write the connect() function at the sender’s end, that makes it possible.

Thanks a lot!

strive_and_conjure | 2021-01-22 16:22