I have a server that has this in the main script:
func _ready() -> void:
get_tree().connect('network_peer_connected', self, 'on_player_connected')
get_tree().connect('network_peer_disconnected', self, 'on_player_disconnected')
When a player quits the game, the server should get the network_peer_disconnected
signal, right? But it doesn't. This is what the on_player_disconnected
function looks like:
func on_player_disconnected(id):
print("Disconnected!")
disconnected_player_info = players[id]
players.erase(id)
So, the print statement should be running, but it isn't. Instead, when the player disconnects, this gets spammed in the server output:
SCRIPT ERROR: update_position: Invalid get index '751147527' (on base: 'Dictionary').
At: res://scenes/main/server.gdc:39
I'm guessing this is because, when the player connects, their unique network ID gets added to a players
dictionary. An a update_position
function uses the ID stored in the dictionary to update the position of the player.
func _physics_process(delta: float) -> void:
$"/root/Server".update_position(int(name), position)
So, the server is trying to call update_position
on an ID that's no longer in players
. They only way the ID could no longer be in the dictionary is that the on_player_disconnected
function is being run, because that removes the player from the dictionary. But the print("Disconnected!")
statement isn't being run, which seriously confuses me.