Sending a signal WITHOUT parameters?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kanor

So I’ve recently made a basic function on one of my GUI scripts. It takes in no arguments, and it’s meant to be used in a lot of places, called by a lot of different sources.

How can I make a signal that usually attaches an argument (like body, or value), and attach it to a function that doesn’t need arguments?

The basic answer would be to have an optional argument that isn’t used in the function:

func exceptionally_useful_function(_placeholder_arg = 10):
    pass

But is there a better way?

:bust_in_silhouette: Reply From: Whalesstate
func _ready()-> void:
	# the fourth arg is called binds also you can add extra binds when it emmits
	connect('resized', self, '_on_resize', ['it_could_be_any_variable', 2, rect_size])
	# after the first arg you can send whatever args you want but give them default values just in case you want to emit the signal without extra binds
	emit_signal('resized', self)
func _on_resize(arg1 : String, arg2 : int, arg3 : Vector2, extra_bind = null)-> void:
	if extra_bind != null:
		print("there's an extra bind")
	# optional use the binds
	match arg2:
		0:
			print('Zero')
		_:
			prints(arg1, arg3)

also you can do it from the editor if you want to send predefined values as extra args, just press on the Advanced button when you connect a signal and add as many args as you want

Whalesstate | 2021-02-26 03:23

Sorry if my question was confusing. I don’t need to ADD arguments to a signal to fit into a function with SEVERAL arguments, I need a signal to REMOVE arguments to fit into a function with NO arguments. exceptionally_useful_function doesn’t use any arguments.

Signals like _on_area2D_entered always pass the node that entered the Area2D in the signal, but if the connected function DOESN’T SUPPORT have any arguments, it causes an error.

Kanor | 2021-02-26 16:59

you can add an underscore before the argument name , then when you emits it pass a variable of type null like on area2d_entered , and in the code just use the arguments when it’s not = null

Whalesstate | 2021-02-26 18:22

hi i had the same issue on a project i’m currently working on , and i found a solution and i thought i’d share it if any one needs it ,
you should use unbind function

example (GD 4.2.1 ) :
Clearance_angle.connect(“item_selected” ,_update_tool_data.unbind( 1 ))

Clearance_angle is an option_button
“item_selected” is the signal wich return an index value
_update_tool_data is the function with no parameters in it
1 is number if prameters (which in this case is 1 )
end of example ;

:v: :v: :v: