How to connect two or more different devices to a server in Godot 4.2.1 (Multiplayer Game)

Godot Version

Godot Engine 4.2.1

Question

Hello, I had a problem with my multiplayer game, when I execute game in my Android phone and my computer and connect them in the server, just I can see one player, my question is: Why does this happen in my game and how can I solve it?

There can be many reasons.

Communication failure due to network security or network configuration.

Various logic issues in code.

User error.

To solve you need to debug.

2 Likes

This is my code of the multiplayer system in my game:

extends Node2D

var peer = ENetMultiplayerPeer.new()
@export var player_scene: PackedScene
const DEFAULT_IP = "localhost"
const DEFAULT_PORT = 135
const MAX_PLAYERS = 4
var save_data = {name = "", position = Vector2(711, 505)}
	
func _add_player(id = 1):
	var player = player_scene.instantiate()
	player.name = str(id)
	save_data["name"] = player.name
	call_deferred("add_child", player)

func _on_create_game_pressed():
	$"Create Game".visible = true

func _on_join_game_pressed():
	$"Join Game".visible = true
	
func del_player(id):
	rpc("_del_player", id)
	
func exit_game(id):
	multiplayer.peer_disconnected.connect(del_player)
	del_player(id)
	
@rpc("any_peer", "call_local") func _del_player(id):
	get_node(str(id)).queue_free()
	
func _on_exit_button_pressed():
	exit_game(save_data["name"].to_int())
	
func _input(event):
	if event.is_action_pressed("ui_cancel"):
		exit_game(save_data["name"].to_int())

func _on_exit_create_game_pressed():
	$"Create Game".visible = false

func _on_join_button_pressed():
	$"Multiplayer Mode".visible = false
	$"Join Game".visible = false
	peer.create_client(DEFAULT_IP, DEFAULT_PORT)
	multiplayer.multiplayer_peer = peer

func _on_join_my_game_pressed():
	$"Multiplayer Mode".visible = false
	$"Create Game".visible = false
	peer.create_server(DEFAULT_PORT, MAX_PLAYERS)
	multiplayer.multiplayer_peer = peer
	multiplayer.peer_connected.connect(_add_player)
	_add_player()
	
func _ready():
	var characters = ["a","b", "c", "d", "e", "x", "y", "z", "w", "A", "B", "C", "D", "E", "F", "G", "X", "W", "K",1,2,3,4,5,6,7,8,9,0]
	var password = ""
	for i in range(characters.size()):
		randomize()
		var choice = characters[randi() % characters.size()]
		password += str(choice)
	$"Create Game/LineEdit".text = str(password)
	$UI.visible = false

I think it is because ENetMultiplayerPeer only supports desktop platforms and they have access to localhost.

This seems low your access to that port may be blocked. You need to use a port above 1024.

This will only allow clients to connect if they are on the same machine. The remote client needs to know your your host’s ip address in order to connect.

You should add a text field to the UI to type an IP before the client presses join.

2 Likes

That doesn’t sound correct. Localhost is not accessible for devices outside a machine.

How can I find a IP address? I’m sorry, I am beginner creating multiplayer games in Godot Engine

Ahh, I understand, localhost is only accessible for the machine where it is being used

Android → wifi settings → ip address

Windows → cmd → ipconfig?

Look at your router device table if you know how to access it.

I have already managed to access the IP address, I just need to verify if everything is working correctly.

I think the host will bind to all network interfaces.
And the create client just needs to use the right IP on the network.

So just modify the default IP to your host for a quick test.

I have already changed IP address of my Wi-Fi connection, now can I try my game?

What happens if my IP address is different on both devices?

When you create host you don’t provide an IP. This is because it will bind to all network interfaces. It doesn’t matter what the IP is as it isn’t important or used. Only the port matters on the host. The host is ready to accept connections of clients. There will be a handshake behind the scenes when a client connects, that will happen automatically, and will get the clients IP.

Only the client needs to know the IP, and port, of the host.

So I should get the IP address of the server of my game or instead I connect my game to IP address of my router?

I’m not sure I understand. it seems like your phone or your computer is the host/server, while the other is the client.

phone: server/host
computer: client (needs to know phone's IP address)
# or
computer: server/host
phone: client (needs to know computer's IP address)

Who ever the server is it already knows its own IP address.

if you notice in the code when you create the server it doesn’t require the IP address.

peer.create_server(DEFAULT_PORT, MAX_PLAYERS)

but when you create the client it does require the IP address.

peer.create_client(DEFAULT_IP, DEFAULT_PORT)

so in order to do a quick test choose the phone or computer as the server, and hardcode the ip of the server for the client to use, in the DEFAULT_IP.

Yes, I understand your explanation, but, how can I get the IP address of every clients for connect them to the same server?

You don’t, you just need one IP, the server’s. Every client will use the same server IP.

server/host ip = 1.2.3.4 port 5555

client1 connect to 1.2.3.4 on port 5555
client2 connect to 1.2.3.4 on port 5555
client3 connect to 1.2.3.4 on port 5555
...
1 Like

Is to say, I must use these data or just is example? I think that should create my own IP for this server. In addition, I had the same problem, my devices doesn’t connect them to play the game

You will be assigned an IP address by your router. It will usually start with 192.168.x.x you will have different IP adresses for each device. On your computer, if windows, run ipconfig; mac/linux ip addr. this will tell you what your computer’s IP address is. You will use this information on your phone to connect to your computer’s game, but your program needs to support typing in an IP address via a LineEditr or TextEdit node.