+1 vote

I am learning how to use Godot's multiplayer support, and I have run into a problem I can't resolve (and can't find a question and answer that fits my problem). I have distilled my problem down to the code I'm posting below. I also have the same code in a ready-to-use Godot project at https://www.soapspangledgames.com/godot/Server.zip

I'm using a custom compiled Godot 3.2 alpha.

Here is my server code (Server.tscn):

extends Node

var m_nPort = 50000
var m_nMaxPlayers = 10

var m_server
var m_aPlayers = Array()

func _ready():

func createServer():
    print("Creating server on port " + str(m_nPort))
    m_server = NetworkedMultiplayerENet.new()

func connectEvents():

func clientConnected(id):
    print("Player connected with id " + str(id))

func clientDisconnected(id):
    print("Player with id " + str(id) + " disconnected")

func showPlayers():
    print("Players:" + str(m_aPlayers.size()))

Here is my client code (Client.tscn):

extends Node

var m_nServerPort = 50000
var m_stServerAddress = "localhost"
var m_client

func _ready():

func connectToServer():
    print("Connecting to server at " + m_stServerAddress + " on port " + str(m_nServerPort))
    m_client = NetworkedMultiplayerENet.new()

func _on_packet_received(id,packet):

The server never registers the connection attempt from the client, as clientConnected(id) is never called. What am I doing wrong?

in Engine by (401 points)

If I run the client and server nodes on separate instances of Godot, then the connection and disconnection are detected. If I run the client and server in the same program, they don't talk. This is problematic, as I want the single player mode to also be client-server, but with the client and server contained within the same executable.

Check the error message from the create_client and create_server methods. I think it does not work because you are already registered as the server and already have a network id and everything.
Another option you can have is to change your connecttoserver in a way that it will check that you are already connected and skip all the setup and stuff for the client. In my specific case I wrapped all my communication within a class and if i want to send to myself, as is your case, my communication class handles it and my client never knows who he is talking to.

Both create_client and create_serverreturn zero. I'm trying to use set_custom_multiplayer on both the server node and the client node to allow the client and server to be in the same scene, but I'm not having any luck.

Interestingly, create_client returns zero even if there is nothing listening on port 50000. I would think a connection failure would result in this circumstance.

Please log in or register to answer this question.

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.