0 votes

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):

But is there a better way?

in Engine by (167 points)

1 Answer

–1 vote
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:
            prints(arg1, arg3)
by (272 points)

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

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. exceptionallyusefulfunction doesn't use any arguments.

Signals like onarea2D_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.

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.