The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I tried to use the custommultiplayer 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 peerconnected signal). The debugger throws an error saying there is no peer connected with the peer id I received by the method OnPeer_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")
Godot version 3.3.2
in Engine by (12 points)

1 Answer

0 votes

you are trying to send a rpc message before the client fully connected, add a yield(gettree(), 'physicsframe')

by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.