• Godot Help
  • How to use the arguments in a custom signal?

This feels like a silly question, but I haven't been able to find a straight answer.

I know that I can create custom signals with arguments like:
signal mySignal(someArgument)

And I can then emit that signal with data in the argument:
emit_signal(someVariable)

But when I connect the signal at the other end, I've got:
$SomeNode.connect("mySignal", self, "functionToRun")

And I'm not sure exactly how to reference the argument I've added to the signal?

I saw another source that suggested:
$SomeNode.connect("mySignal", self, "functionToRun", [$SomeNode.someVariable])

But I'm not really clear on how I'm supposed to reference the argument in the functionToRun().

I assumed it was going to pass the argument to whatever argument is declared in the function.

For example:

func functionToRun(localArgument):
	# And when I reference "localArgument" in the function, it uses the value passed from the signal.
	pass

But it doesn't seem to be doing that.

Can anyone shed some light on how this works? Even a link to a working example would be amazing.

  • DaveTheCoder replied to this.
  • cybereality

    Thanks for the reply. I did look at the documentation before posting, but it didn't seem very clear about what to do on the receiving end.

    But from your comment, I get the impression that is should be something like this...

    On the emitting node:

    var argumentVariable = 100
    signal mySignal(signalArgument)
    
    func emitting_function():
    	emit_signal("mySignal", argumentVariable)

    On the receiving node:

    func _ready():
    	$EmittingNode.connect("mySignal", self, "on_signal_received")
    
    func on_signal_received(functionArgument):
    	# In here functionArgument will be the value of argumentVariable
    	# because it is passed along as signalArgument?
    	pass

    Do I have that right? I thought I tried this and it didn't work, but if this is the correct implementation then I must have made a mistake elsewhere.

    trevbot But it doesn't seem to be doing that.

    Provide more detail. What's the output if you print the argument?

    cybereality

    Thanks for the reply. I did look at the documentation before posting, but it didn't seem very clear about what to do on the receiving end.

    But from your comment, I get the impression that is should be something like this...

    On the emitting node:

    var argumentVariable = 100
    signal mySignal(signalArgument)
    
    func emitting_function():
    	emit_signal("mySignal", argumentVariable)

    On the receiving node:

    func _ready():
    	$EmittingNode.connect("mySignal", self, "on_signal_received")
    
    func on_signal_received(functionArgument):
    	# In here functionArgument will be the value of argumentVariable
    	# because it is passed along as signalArgument?
    	pass

    Do I have that right? I thought I tried this and it didn't work, but if this is the correct implementation then I must have made a mistake elsewhere.

    I believe so, yes. You should try it and see if there are any errors.

    @trevbot

    Hello, I tried it and got it to work by:

    1. On Emitting Node: adding emitting_function() to _process().
    func _process():
    	emitting_function()
    func emitting_function():
    	emit_signal("mySignal", argumentVariable)
    1. Make sure Emitting Node is the child of Receiving Node.

    2. On Receiving Node: Do a print test on_signal_received().

      func on_signal_received(functionArgument):
      	print(functionArgument)

    In this case it should print 100 over and over again.

    From what I understand it should be something that triggers the emitting function. So for testing I put it in _process() which will do that in every frame.

      So I tried the format I clarified above (which seems similar to what Gowydot is recommending), and it works.

      Guess I just overthought it. 😅 I was expecting to have to specify the argument in the connect() call but that all seems built-in.

      Also, the emitting node doesn't have to be a child of the receiving node if you use get_node() or another similar method to specify a node.