Is there a way to kick/disconnect player that joined by NetworkedMultiplayerENet

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rud0lf
:warning: Old Version Published before Godot 3 was released.

I recently became interested in multiplayer games. Apparently, godot 2.2 (and as far as i know godot 3.0) provides no ability to kick/disconnect peer. Let’s say i start the server limited to 2 players, with address known to group of people, and i want to disconnect player that sent incorrect password (which is obtained with remote call). The idea of simply not registering player and assigning in-game object to them is not enough, because they gonna stall the available connection slots, so no more players will be able to join. I really, really don’t want to code networking logic by myself based on PacketPeer. Is there possibility do disconnect player basing on their peer_id ?

:bust_in_silhouette: Reply From: rud0lf

I’m answering myself because I’ve found both answer and solution. After looking through code my answer is: no, but… But since Godot uses ENet implementation which provides certain functions, I’ve written small modification for Godot 2.2 and (hopefully) Godot 3.0. It extends ENet functionality with two functions: one to disconnect player by his id, and another one to obtain peer IP address by his id. The code is here. It has been tested with Godot 2.2 but not with 3.0 (my graphic card does not support OpenGL 3). Here’s the sample how it can be used to blacklist/ban certain IPs:

server.gd

var blacklist = [“127.0.0.1”, “192.168.10.44”]

_ready():
get_tree().connect(“network_peer_connected”, self, “_client_connected”)
# …

func _client_connected(id):
var adr = get_tree().get_network_peer_address(id)
assert(not adr.empty())
if adr.ip in blacklist:
rpc_id(id, “kicked”, “you have been banned from this server”)
get_tree().disconnect_network_peer(id)

client.gd

remote kicked(reason):
show_info("Kicked from server: " + reason)

Does not work. Get the error: "Invalid call. Nonexistent function 'disconect_network_peer' in base 'SceneTree'.

Sojan | 2019-07-12 14:18

:bust_in_silhouette: Reply From: zombieCraig

I’m aware this is an old question but I figured I would update the answer as the answer is different now and not always obvious.

# Initialize your peer (peer is declared elsewhere)
peer = NetworkedMultiplayerENet.new()
....
# Somewhere in your Code where you want to kick, where remote_rpc_id
#  is the ID you get when a player connects 
peer.disconnect_peer(<remote_rpc_id>)