Multiplayer Skeleton Not Working

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

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():
	createServer()
	connectEvents()
	
func createServer():
	print("Creating server on port " + str(m_nPort))
	m_server = NetworkedMultiplayerENet.new()
	m_server.create_server(m_nPort,m_nMaxPlayers)
	get_tree().set_network_peer(m_server)

func connectEvents():
	m_server.connect("peer_connected",self,"clientConnected")
	m_server.connect("peer_disconnected",self,"clientDisconnected")

func clientConnected(id):
	print("Player connected with id " + str(id))
	m_aPlayers.append(id)
	showPlayers()
	
func clientDisconnected(id):
	print("Player with id " + str(id) + " disconnected")
	m_aPlayers.erase(id)
	showPlayers()

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():
	connectToServer()

func connectToServer():
	print("Connecting to server at " + m_stServerAddress + " on port " + str(m_nServerPort))
	m_client = NetworkedMultiplayerENet.new()
	m_client.create_client(m_stServerAddress,m_nServerPort)
	get_tree().set_network_peer(m_client)
	get_tree().multiplayer.connect("network_peer_packet",self,"_on_packet_received")

func _on_packet_received(id,packet):
	print(packet.get_string_from_ascii())

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

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.

stormreaver | 2019-12-18 12:48

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 connect_to_server 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.

tastyshrimp | 2019-12-18 13:32

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.

stormreaver | 2019-12-19 12:23

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.

stormreaver | 2019-12-19 13:12