The function 'connect()' returns a value, but this value is never used.

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

Hi All. I constantly see multiple instances of this warning in the debugger and it annoys the hell out of me…

In _ready(), I’m connecting several signals to functions in a singleton script. For each of these connections I get a warning like this up front. Indeed, the signals aren’t emitted until something happens in the game that will emit them but… so what?

Do I need to move the connect’s to the functions that emit the signals?

:bust_in_silhouette: Reply From: Drogrant

Hi.

This warning has nothing to do with when the signal is emitted. It means that the function “connect” returns something, more precisely it returns a value of the Error enum (you can go check the details Here ), and you are not using that value.

I’ll guess that your code is something like this:

func _ready():
      connect('whatever')
      #rest of the function

Thus the returned error value is discarded and the warning raised.

What you can do is use the error for debug checks or something similar or you can ignore the warning by clicking in the warning triangle and selecting ignore warning.

Can i somehow ‘use’ the enum value to hash both the debug warning and the ‘ignored warning’ comment that shows up in the code when i ignore (which is no less irritating if im being honest)…?

Macryc | 2020-07-29 10:55

If you create a variable just for the return value maybe the editor will warn you that you created a variable and then never used it.

If those warnings really annoy you that much you can disable them here:
Project Settings → Debug → GDScript → Warnings
But they will also disappear with other functions.

Do what makes you work more comfortable.

Drogrant | 2020-07-29 11:03

So, can I just as safely ignore it ?

devAS | 2021-01-09 10:19

If you do not want to use the returned value for debug reasons then yes you can ignore it.

Drogrant | 2021-01-11 11:18

:bust_in_silhouette: Reply From: ChetJones003

A simple fix I found is to set the signal equal to an arbitrary variable.

var _random_var = connect("signal", self, "signal_function")

Hope this helps.

Hi there… I use this fix aswell, but I really wanted to understand what it means exactly and if it is the right way to do it…

connect() will complain that value is never used
so,
var random_var = connect() will complain that the variable is never used
and finally,
var _random_var = connect() will stop the warning

but what happened there? this fix also works for ternary if’s… what am I storing in a variable and howcome a dash tell its ok that is never used… I get a little confused about the right way to respond to this issue

Surtarso | 2021-02-22 14:12

In this case, the underscore is just there to tell the compiler “Hey, I don’t intend to use this variable.”
I believe this is called a Dummy Variable or Discard.

Godot actually has some warnings that recommend doing this, in particular:
The argument ‘delta’ is never used in the function ‘physicsprocess’. If this is intended, prefix it with an underscore: ‘_delta’

So it’s definitely fine to continue doing this, and I personally prefer it over the “Ignore warning” button.

Urakka | 2023-04-03 01:19

:bust_in_silhouette: Reply From: Surtarso

connect() will give you a value never used warning

var random_var = connect() will give you a variable never used warning

var _random_var = connect() will stop the warning and the variable/value discarted

BUT, this will store the value in a variable and use that variable to print an error if it happens and will also stop the warning, while not stopping errors to be shown.
Error code 0 means no errors

var error_code = connect()
if error_code != 0:
	print("ERROR: ", error_code)

This will also happen with some Ternary IF’s for some reason, same “fix” applies.

var _run = player_sprite.play("running_suit") if PlayerData.has_suit else player_sprite.play("running")

Wish I could understand this better aswell

Surtarso | 2021-02-22 14:33

because ternaries were never meant for calling functions, they are for getting one value or another

var estimate = "good" if score > 5 else "bad"

if you want to call one function or another use if-else

if PlayerData.has_suit:
    player_sprite.play("running_suit")
else:
    player_sprite.play("running")

badunius | 2022-10-05 22:20

:bust_in_silhouette: Reply From: opyate

Disable the warning for this one instance:

func myfunc():
    foo = 'bar'
    # warning-ignore:return_value_discarded
    the_thing_that_returns()

More about warnings and disabling them here: GDScript warning system — Godot Engine (stable) documentation in English

I like this solution as it documents the code and tells the reader of the code that you made a conscious decision to suppress and are aware a return value normally exists.

Mison | 2021-05-29 11:23

:bust_in_silhouette: Reply From: kismet

This question is old, but I ran across it while searching for info on the same problem because I wanted to see if there was a standard way of handling this.

It doesn’t look there is.

I found the best way is to wrap them in an assert. It’s an inline solution that gets rid of the warning and will let you know if there is a problem with your signal connection which you probably want to know about anyways.

Example:

assert(get_tree().connect("peer_connected", self, "_peer_connected") == 0)

Creating vars for each signal and then leaving them to be cleaned up or checking their values is a lot of pointless extra code.

Ignoring warnings is almost always a bad idea.

Don’t do this!
I found out the hard way that asserts are removed in release builds which will break your game.

From the docs:

The assert keyword can be used to check conditions in debug builds. These assertions are ignored in non-debug builds. This means that the expression passed as argument won’t be evaluated in a project exported in release mode. Due to this, assertions must not contain expressions that have side effects. Otherwise, the behavior of the script would vary depending on whether the project is run in a debug build.

Source: GDScript reference — Godot Engine (stable) documentation in English

Jibby | 2021-11-20 00:52

Thanks for the clarification @jibby

d2clon | 2022-12-22 18:20

:bust_in_silhouette: Reply From: idbrii

If you want to get an error when a connect call fails, you can make a validate module with a static function that I can reuse anywhere connect (or other Godot API functions) shouldn’t fail:

# validate.gd
static func ok(err):
    assert(err == OK)

# somethingwithbutton.gd
const validate = preload("res://code/util/validate.gd")
func _ready():
        validate.ok(btn.connect("pressed", self, "_on_btn_pressed"))

Similar to @kismet’s idea of using assert except this function call won’t get removed in builds.

Looks really cumbersome as you can just use an object’s call function

btn.call("connect", "pressed", self, "_on_btn_pressed")

Nice one-liner that also shows error if something goes wrong

Wakatta | 2023-01-06 17:18

@Wakatta That eliminates the lint warning, but you won’t get an error when connection fails.

idbrii | 2023-01-06 22:36

Make sure you don’t have warnings disabled.
Because it reports connection failures just fine with a warning

Wakatta | 2023-01-07 20:29