Godot 4 disconnect player

Godot Version

v4.2.1.stable.mono.official [b09f793f5]

Question

So, I am trying to disconnect a player in multiplayer but I can’t get it to work.

extends Node2D

var peer = ENetMultiplayerPeer.new()
@export var player_scene: PackedScene

func _enter_tree():
if Playervars.play_state == 1:
_on_host()
if Playervars.play_state == 2:
_on_join()

func _process(_delta):
if Input.is_action_just_pressed("ui_cancel"):
get_tree().change_scene_to_file("res://main.tscn")

func _on_host():
peer.create_server(135)
multiplayer.multiplayer_peer = peer
multiplayer.peer_connected.connect(_add_player)
_add_player()

func _add_player(id = 1):
var player = player_scene.instantiate()
player.name = str(id)
call_deferred("add_child",player)

func exit_game(id):
multiplayer.peer_disconnected.connect(del_player)
del_player(id)

func del_player(id):
rpc("_del_player",id)

@rpc("any_peer", "call_local") func _del_player(id):
get_node(str(id)).queue_free()

func _on_join():
peer.create_client("localhost", 135)
multiplayer.multiplayer_peer = peer

Please elaborate what is happening and what you expect to happen. Are you trying to drop a connection like kicking a player from the server? Do players whom disconnect still persist in the game?

You should connect peer_disconnected as part of _on_host, that’s for the server to handle clients disconnecting i.e. cleaning up players that left the game.

1 Like

Just normal disconnect. The player leaving the game. A player leaves when they change to main.tscn but they still show on other players’ screens.

Then I believe my second paragraph will fix that issue.

func _on_host():
    peer.create_server(135)
    multiplayer.multiplayer_peer = peer
    multiplayer.peer_connected.connect(_add_player)
    # server must handle disconnects from startup to shutdown
    multiplayer.peer_disconnected.connect(del_player)
    _add_player()

And make sure the player calls disconnect_peer when going to main.tscn

if not multiplayer.is_server():
    # 1 is always the server
    multiplayer.multiplayer_peer.disconnect_peer(1)
1 Like

It says I can’t call is_server on a null value. Also, wouldn’t disconnect_peer(1) remove the peer with id 1? That would be the server host, right?

Not sure on the null value, any Node should have a multiplayer value. Where are you trying to call it?

Yes disconnect_peer(1) disconnect from the server, it doesn’t remove the server just the connection from said peer. The if statement should confirm we are not the server, if we want to close the server we would call multiplayer.multiplayer_peer.close()

1 Like

Oh I got it to work. Just had to add

func _process(_delta):
	if Input.is_action_just_pressed("ui_cancel"):
		multiplayer.multiplayer_peer.disconnect_peer(1)
		get_tree().change_scene_to_file("res://main.tscn")

and

func _on_host():
	peer.create_server(135)
	multiplayer.multiplayer_peer = peer
	multiplayer.peer_connected.connect(_add_player)
	multiplayer.peer_disconnected.connect(del_player)	
	_add_player()

and now it works. Thanks!

I also notice your port number is below 1024 making it require admin permissions to run the server, is this intentional?

Umm. Idk? This is my first game so I don’t know what to set the port too. I just did what the tutorial did. Also 1 more tiny question: I use peer.close() when the host leaves, but how do I then make the client return to the homescreen? rpc? It’s what I did to reflect kick changes.

To handle server closures the clients should connect to the multiplayer.server_disconnected signal.

multiplayer.server_diconnected.connect(func():
    get_tree().change_scene_to_file("res://main.tscn"))

Ports are made up but they have to be from 1024 to 65535, anything <1024 is considered reserved for system administrator use. I host a lot of game servers from my house and it’s crazy keeping track of all the ports.

Ok! Your code worked! About the ports. I’m going to a minecraft like thing so that if for some reason someone wants to host a server, they can. It may be switched off ports because I don’t want people to have to be on the same network. I have a raspberry pi in which I hope to use as a server. I also want people to be able to host from their device to people on the same network, also like minecraft. So I don’t even know if ports are the right way to do this.

Ports are simply a requirement to host anything. I believe your are thinking of matchmaking, or a central server (still has a port assigned) to connect players or pass players along to smaller more dispersed servers (also ports). If you are hosting this from your home you will need to port forward, this tells your router when it recieves packets for a certain port to send it to your raspberrypi.

In Godot you would hard-code your public IP address to achieve this, but it’s best to include a manual ip address entry in case someone wants to host their own server or your internet service provider randomly changes your public IP.

Good luck! Hosting is fun!

1 Like