Network signals return a value but it never used

Godot Version



I am trying to make a multiplayer game but i made a autoload file and put what possible io codes, but nothing happened, it returns a warning:
“The function ‘connect()’ returns a value, but this value is never used.”.

func _ready():
get_tree().connect(“network_peer_connected”, self, “_player_connected”)
get_tree().connect(“network_peer_disconnected”, self, “_player_disconnected”)
get_tree().connect(“connection_failed”, self, “_connected_fail”)
get_tree().connect(“server_disconnected”, self, “_server_disconnected”)

The warning is just telling you that the Object.connect() function returns a value but that value is never used. The warning has nothing to do with if the code work or not. Your issue is in another place.

This is a script I put it in autoload, so it must work when I start the game, but none of its functions work:

func _player_connected(id: int):
	print("Player: " + str(id) + " Connected")

func _player_disconnected(id: int):
	print("Player: " + str(id) + " Disconnected")

func _connected_fail():
	print("Failed to connect")
func _server_disconnected():
	print("Server Disconnected")

If these functions are in an autoload they can only be accessed via prefixed autoload name.
For example if the autoload name is Global then _player_connected can only be accessed by the call Global._player_connected

Unless the code you showed in func _ready() is in that same autoload, the connecting string won’t point to it.
Is that _ready() code in the same autoload node?

You can use that return value to check if the connection actually worked:

if get_tree().connect(“network_peer_connected”, self, “_player_connected”) != 0:   
    print("error in connecting")

These functions are with the _ready() in the same script, and when I used your code it prints “error in connecting”.

print the error number out as well although they are very generic and not usually much help:

var err = get_tree().connect(“network_peer_connected”, self, “_player_connected”)
if err != 0:   
  printt("error in connecting",  err)    

The errors are listed here

I can’t see anything wrong with what you have.
You could try a blank project and only create a lobby node with this code and check if that code connects without error.

I created a new project, and typed your code, but when I but it alone it works:

func _ready():
	var err = get_tree().connect("network_peer_connected", self, "_player_connected")
	if err != 0:   
	  print("error in connecting",  err)

But when I use it after connecting signals it doesn’t causes an error (err = 31):

func _ready():
	get_tree().connect("network_peer_connected", self, "_player_connected")
	get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
	get_tree().connect("connection_failed", self, "_connected_fail")
	get_tree().connect("server_disconnected", self, "_server_disconnected")
	var err = get_tree().connect("network_peer_connected", self, "_player_connected")
	if err != 0:   
	  print("error in connecting",  err)

try this

self.connect("network_peer_connected", self, "_player_connected")

I tried to simulate it in my project and it worked


signal network_peer_connected(param)

func ready():
    self.connect("network_peer_connected", self, "_player_connected")

func _player_connected(_param):
    print("Connected ", _param)

somewhere call

Global.emit_signal("network_peer_connected", "something")

This should error because you are trying to connect the same signal twice.

Try this code:

func _ready():
    var err
	err = get_tree().connect("network_peer_connected", self, "_player_connected")
	if err != 0:   
	  print("error in network_peer_connected signal",  err)
	err = get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
	if err != 0:   
	  print("error in network_peer_disconnected signal",  err)
	err = get_tree().connect("connection_failed", self, "_connected_fail")
	if err != 0:   
	  print("error in connection_failed signal",  err)
	err = get_tree().connect("server_disconnected", self, "_server_disconnected")
	if err != 0:   
	  print("error in server_connected signal",  err)