Signal with parameter, sometimes I need to bind it, sometimes not?

Godot Version

4.3 stable

Question

I use signals in my code and just notices something, that I don’t really understand, maybe someone can help. Sometimes I need to use .bind() to use a parameter in a signal method, sometimes I don’t need to use it, examples:

This works:

enemy.combat_started.connect(_on_enemy_combat_started)

func _on_enemy_combat_started(p_enemy: Enemy) -> void:

This does not work:

_dice.pressed.connect(_on_dice_pressed)

func _on_dice_pressed(p_dice: Dice) -> void:

Instead I have to write this:

_dice.pressed.connect(_on_dice_pressed.bind(_dice))

func _on_dice_pressed(p_dice: Dice) -> void:

I wonder why that’s the case?

“enemy” is an Area2D that exists in my node tree in the Editor, “_dice” is a Button that is instanced at runtime.

It depends on if the signal emits parameters or not, it would seem that combat_started emits a Enemy, while pressed does not emit a Dice

The p_enemy passed into the connected function is probably not the same one whose signal you are connecting.

Are you sure combat_started works as you think it does?

I think you were correct!

My Enemy script looked like this:

signal combat_started(enemy: Enemy)

func _on_body_entered(p_body: Node2D) -> void:
	combat_started.emit(self)

I removed the parameter from the signal signature and the emit call, now I have to bind the parameter in the connect call in my Main script as expected.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.