Hey all. Working with some simple networking code and can't find how to disconnect the player when I close the client side program. When I run the server side and client side, if I stop or close the client program it never sends the disconnect signal to the server. So any code on that player still runs, but can't send it anywhere. On the server side I need to remove the player when they disconnect.

How do I run the disconnect code on the client side when I close the program?

How can I check to make sure a player is connected before trying to do anything with it?

Edit: I found the answer! Turns out I hadn't linked the "peer_disconnected" signal to anything. It was just sitting there doing nothing so once I hooked it up, I can work with a disconnected player!

award yeah I saw that in the docs, but was hoping there was a way to run a func whenever you close down the program. I guess I'll have to go with the heartbeat method then.

Is there any documentation on how to run a heartbeat in Godot? I'm not finding any info on it, but also not sure if I'm searching for the wrong thing.

    Just keep the server side running persistently, and send a custom heartbeat req/ack packet every few second to test if connected clients are still responding.

      CorvaNocta but was hoping there was a way to run a func whenever you close down the program.

      What if the program closes or the connection is terminated in an emergency? Heartbeat is more reliable.

        MagickPanda that sounds pretty simple. Is there a built in code for that or do I just build it myself with an rpc call?

        Tomcat yeah I think I need to build that in. Not finding any info on a built in heartbeat system though which is unfortunate

        MagickPanda I found the answer! And a little embarrassed lol. I had the "peer_disconnected" code in there, but I never actually connected the signal for it to anything. So the player was disconnecting, and firing the signal for that disconnect, but I never did anything to link that signal to an actual process. Seems to be working fine now!

          CorvaNocta That only works when a peer is disconnected gracefully I think, you still need a check to test against erratic disconnection such as network failure or client power down etc.
          Though I am not 100% sure about this on_disconnected callback's implementation taking edge cases into consideration.

            6 months later

            MagickPanda As far as i am aware, in 4.3 mayb ein 4.2 also, when peer closes the application it automatically sends peed_disconnected() signal.

            Hope it helps.

            Im on the other hand struggling figuring out how to disconnect server. Right now i have a lobby type thing, where one player hosts, the other joins.

            It players that are not host disconnect then everything is fine, but how to handle when host disconnects? I would like to keep the lobby, and then one of the player gets to be host then in that event..

            Also i cant find docs on server disconnect. i mean there is multiplayer.disconnect_peer() but there is no multiplayer.disconnect_host()..