What's the use of signal parameters/arguments?

Godot Version

4.1.1

Question

This is what the documentation says:

// Additional arguments may be declared.
// These arguments must be passed when the signal is emitted.
signal item_dropped(item_name, amount)

But you can emit signals without passing them, there’s no restriction.

signal item_dropped(item_name, amount)

func _ready():
	item_dropped.connect(_on_item_dropped)
	item_dropped.emit()

func _on_item_dropped():
	print("you dropped nothing")

you can choose not to declare them

signal item_dropped

func _ready():
	item_dropped.connect(_on_item_dropped)
	item_dropped.emit("coins", 10)

func _on_item_dropped(item_name, amount):
	print("you dropped ", amount, " ", item_name)

is their use just to help the user know the right arguments to pass?

is their use just to help the user know the right arguments to pass?

Yes, and if you provide their static types, that information will be included too. For example, when the editor creates a new signal connection function for you.

1 Like

thanks,

you mean if I do this?

signal item_dropped(value1: String, value2: String)

it still shows as a var in the editor though, and when i create a new connection it does add the parameter names, but not the static typing if I’m doing it right

As far as I am aware statically typed signal parameters has not been not implemented yet. This syntax won’t throw any errors, but it is also not enforced (so basically the types you provide are simply ignored).

1 Like

Do you happen to know if it’s a planned feature or not ?

Not aware.

Perhaps @vnen knows something about it?

Signals do not check the arguments when emitting nor when connecting. This is the same for signals in native nodes as well, you could emit a toggled signal from a button without passing any argument and nothing will stop you.

You will get errors if there is a mismatch in arguments only when the connected function is actually called.

I’ve seen discussions about enforcing this at connection/emission, but nothing concrete. Since it’s not done by the core it is a bit tricky to do on the GDScript side.

2 Likes