Using custom_multiplayer makes peer not to connect imediatly.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DCWarrior

I tried to use the custom_multiplayer instead of the built in MultiplayerAPI already in the tree scene so I could later adapt to a future project I plan to work on, however I noticed that if I do so, I can’t imediatly make a rpc call to a client that connects to the server (signaled by the peer_connected signal). The debugger throws an error saying there is no peer connected with the peer id I received by the method _On_Peer_Connected. I tried to get the connected peers in the same method and the result comes out as an empty array. One solution I found was to wait for a very brief period of time before I try to make a rpc call and it works just fine. Does anyone know why this happens? And if so how can I solve it?

It’s still worth noticing that if I use the MultiplayerAPI of the tree scene in the server side I don’t need to wait, it works perfectly that way.

Server code:

extends Node

onready var ServerHub = get_parent()

var network = NetworkedMultiplayerENet.new()
var gateway_api = MultiplayerAPI.new()

# Variables regarding the connection to the client
var max_players = 5

func _ready():
	var port = 4001
	print("Port used: " + str(port))
	network.create_server(port, max_players)
	set_custom_multiplayer(gateway_api)
	custom_multiplayer.set_root_node(self)
	custom_multiplayer.set_network_peer(network)

	network.connect("peer_disconnected", self, "_On_Peer_Disconnected")
	network.connect("peer_connected", self, "_On_Peer_Connected")

func _process(delta):
	custom_multiplayer.poll()

func _On_Peer_Disconnected(player_id):
	print("Player: " + str(player_id) + " disconnected from the server")
	
func _On_Peer_Connected(player_id):
#	yield(get_tree().create_timer(0.000000000001), "timeout")
	print("Player: " + str(player_id) + " connected to the server")
	rpc_id(player_id, "Hello")
	print("Peers connected: " + str(custom_multiplayer.get_network_connected_peers()))

Client code:

extends Node

var network = NetworkedMultiplayerENet.new()
var gateway_api = MultiplayerAPI.new()

# Variables regarding the connection with the game server
var ip = "127.0.0.1"

func _ready():
	network.create_client(ip, 4001)
	set_custom_multiplayer(gateway_api)
	custom_multiplayer.set_root_node(self)
	custom_multiplayer.set_network_peer(network)

	network.connect("connection_failed", self, "_On_Connection_Failed")
	network.connect("connection_succeeded", self, "_On_Connection_Succeeded")

func _process(delta):
	custom_multiplayer.poll()

remote func Hello():
	print("Hello")

func _On_Connection_Failed():
	print("Failed to connect to game server")
	
func _On_Connection_Succeeded():
	print("Succesfully connected to game server")
:bust_in_silhouette: Reply From: galo

you are trying to send a rpc message before the client fully connected, add a yield(get_tree(), ‘physics_frame’)