0 votes

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 onslidervalue_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.

Godot version Godot 3.2.1
in Engine by (26 points)

1 Answer

+3 votes
Best answer

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)
by (642 points)
selected by

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

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

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.