Godot 4 signals with bound arguments - help heeded

Godot Version

4.2

Question

Hi all. I need some help with signals and arguments.
I have:

  • a global script where I declare global signals (globalsignals.gd)
  • node A which emits a global signal
  • node B which receives it

In Node B’s script I say:

func _ready():
var argument: int
globalsignals.my_global_signal.connect(method_to_call.bind(argument))

func method_to_call(argument):
print(argument)

In Node A’s script:
globalsignals.emit_signal("my_global_signal", 1)

Judging by the docs, Node A should be emitting the global signal along with the argument 1. What am I doing wrong in Node B that the argument is not getting bound to the callable and not being passed to it?
Thank you.

Try

globalsignals.my_global_signal.emit(1)

1 Like

tried that already, doesn’t work. The problem is with the connector, I think, not the emission. And how to correctly bind the argument.

A little more context wouldn’t hurt. What does your declaration of globalsignals.my_global_signal look like?

Separate thought: could it be an ordering issue of when the connection is setup vs. when it’s emitted? What are you seeing in node b? Is it crashing? Is it getting a signal and somehow no argument? A wrong argument? Nothing at all?

Callable.bind() is used to bind extra parameters to a Callable and are passed after the required parameters of that Callable. If your Signal already emits with 1 parameter you don’t need to bind anything else.

func _ready():
	globalsignals.my_global_signal.connect(method_to_call)

func method_to_call(argument):
	print(argument)
4 Likes

That’s what I thought too, but it doesn’t work. THe Callable doesn’t get called when it’s declared with an argument like this: func callable_method(argument) when the signal that is connected to it has no bound argument.

The signal gets picked up but the method doesn’t trigger.

Then the issue is what @Delphi pointed out. You may be emitting the signal before it getting connected.

I think that is the listener received arguments more than it needs.
It’s same in Callable.callv, if i do this:

func _ready() -> void:
    foo.callv([1, 2])


func foo(a: int) -> void:
    print(a)

It will not works because the foo func can’t accept more arguments.

In Callable.bind, it will concat arguments in the end in call.
In your case, it means this:

func _ready():
    # in node A
    # first arg: signal.emit passed
    # second arg: bind passed
    foo.callv([1, 1])


func foo(argument):
    # in node B
    print(argument)

It will not work.

thanks a lot, I was looking for some answer like this, cause I don’t use binds in a few methods and it works, the reason is that only has 1 arg and just make total sense that just applies to multiple parameters!