Godot Version
4.5.1
Question
I am trying to create a multiplayer lobby using Godot’s high-level multiplayer API. The project consists of a start scene and a lobby scene. As shown in Fig. 1 and 2, when the Start Game button is pressed, the scene switches to the lobby and a server is created; when Join Game is pressed, the scene switches to the lobby and a client is created connecting to the server.
For simplicity, I have hard-coded the IP address (127.0.0.1) and port (9999) in the multiplayer manager, which is implemented as a global autoload.
After switching to the lobby scene, the server is automatically present in the lobby with its name set to its peer_id, which is 1. In another instance, when Join Game is pressed, the client connects to the server and is spawned via MultiplayerSpawner.
The problem arises when I try to implement the “Quit” functionality (as shown by the Quit button in Fig. 2.).
For the “Quit“ functionality, what I want are:
When the Server hits “Quit“:
All Clients and the Server get disconnected and return to Start Scene.
When a Client hits “Quit“:
This Client get disconnected and return to Start Scene and the player of this client will be deleted from the Lobby Scene on Server.
The way I implement “Quit“:
I use multiplayer.multiplayer_peer.close() to close the connection of a peer.
And through some experimentation, I found:
if I call this on client, the peer_disconnected signal will be fired on server, and server_disconnected signal will be fired on the client itself.
If I call this on server, the peer_disconnected signal will be fired on all client, and server_disconnected signal will be fired on server. (I believe this is different from the documentation. See Fig. 8)
So, for “Quit“ on Client:
- Client: Call
multiplayer.multiplayer_peer.close() - Server: handle peer_disconnected, queue free the player via the client peer_id
- Client: handle server_disconnected, use get_tree().change_scene_to_file() to change the scene.
For “Quit“ on Server:
- Server: Call
multiplayer.multiplayer_peer.close() - Client: handle server_disconnected, use get_tree().change_scene_to_file() to change the scene.
- Server: handle server_disconnected, use get_tree().change_scene_to_file() to change the scene.
After implementing the idea above, the client behaves as expected. When a client presses “Quit”, the player is correctly removed on the server, and the client transitions back to the start scene.
However, the server side has some issues. There are two scenarios:
- When there are no clients connected to the server, pressing “Quit” works as expected: both the server and the client return to the start scene without any issues.
- When one or more clients are connected to the server, pressing “Quit” appears to work correctly from a visual perspective—the scene switches back to the start scene as intended. However, several errors are reported in the Godot debugger. These errors are shown in Figs. 4, 5, and 6, and they all point to the same line of code shown in Fig. 7.
Notably, there is apparently no multiplayer-related logic being used on that line.











