I can't get multiplayer working (Godot 4)

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

Hello, so I’m trying to make a multiplayer game in Godot. I tried following a tutorial on godot networking I couldn’t get the game working (except on “localhost”). After that i downloaded a networking demo-project, but it’s not working eather (except on “localhost”).

Here is the code from both projects:
1

extends Node

@onready var main_menu = $CanvasLayer/MainMenu
@onready var adress_entry = $CanvasLayer/MainMenu/MarginContainer/VBoxContainer/IpEdit

const Player = preload("res://Scenes/Prefabs/Player.tscn")
const port = 9912
var enet_peer = ENetMultiplayerPeer.new()
var is_host = false

var is_mouse_locked = false

func _ready():
	print("Hello world!")

func _input(event):
	if Input.is_action_pressed("quit"):
		get_tree().quit()
	if Input.is_action_just_pressed("lock_mouse"): #DEBUG ONLY
		if not is_mouse_locked:
			Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
			is_mouse_locked = true
		else:
			Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
			is_mouse_locked = false


func add_player(peer_id):
	var player = Player.instantiate()
	player.name = str(peer_id)
	add_child(player)
	print(multiplayer.get_peers())


func  remove_player(peer_id):
	var player = get_node_or_null(str(peer_id))
	if player:
		player.queue_free()


func upnp_setup():
	var upnp = UPNP.new()
	
	var discover_resoult = upnp.discover()
	assert(discover_resoult == UPNP.UPNP_RESULT_SUCCESS, "UPNP Discover Failed! Error %s" % discover_resoult)
	assert(upnp.get_gateway() and upnp.get_gateway().is_valid_gateway(), "UPNP Invalid Gateway!")
	
	var map_resoult_udp = upnp.add_port_mapping(port, 0, "Godot FPS test UDP", "UDP", 86400)
	var map_resoult_tcp = upnp.add_port_mapping(port, 0, "Godot FPS test TCP", "TCP", 86400)
	assert(map_resoult_udp == UPNP.UPNP_RESULT_SUCCESS, "UPNP Port mapping Failed! Error %s" % map_resoult_udp)
	if not map_resoult_udp == UPNP.UPNP_RESULT_SUCCESS:
		upnp.add_port_mapping(port, port, "", "UDP")
	if not map_resoult_tcp == UPNP.UPNP_RESULT_SUCCESS:
		upnp.add_port_mapping(port, port, "", "TCP")
	print("Success! Join Address: %s" % upnp.query_external_address())
	is_host = true


func _on_host_pressed():
	print("HOSTING!")
	main_menu.hide()
	enet_peer.create_server(port)
	multiplayer.multiplayer_peer = enet_peer
	multiplayer.peer_connected.connect(add_player)
	multiplayer.peer_disconnected.connect(remove_player)
	add_player(multiplayer.get_unique_id())
	upnp_setup()


func _on_Join_pressed():
	print("Joining")
	main_menu.hide()
	enet_peer.create_client(adress_entry.text, port)
	multiplayer.multiplayer_peer = enet_peer
	print(multiplayer.multiplayer_peer)
	print(multiplayer.get_peers())
	print(adress_entry.text)

2

extends Node3D

var multiplayer_peer = ENetMultiplayerPeer.new()

const PORT = 9999
const ADDRESS = "192.168.1.110"

var connected_peer_ids = []
var local_player_character

func _on_host_pressed():
	$NetworkInfo/NetworkSideDisplay.text = "Server"
	$Menu.visible = false
	multiplayer_peer.create_server(PORT)
	multiplayer.multiplayer_peer = multiplayer_peer
	$NetworkInfo/UniquePeerID.text = str(multiplayer.get_unique_id())
	
	add_player_character(1)
	
	multiplayer_peer.peer_connected.connect(
		func(new_peer_id):
			await get_tree().create_timer(1).timeout
			rpc("add_newly_connected_player_character", new_peer_id)
			rpc_id(new_peer_id, "add_previously_connected_player_characters", connected_peer_ids)
			add_player_character(new_peer_id)
	)


func _on_join_pressed():
	$NetworkInfo/NetworkSideDisplay.text = "Client"
	$Menu.visible = false
	multiplayer_peer.create_client(ADDRESS, PORT)
	multiplayer.multiplayer_peer = multiplayer_peer
	$NetworkInfo/UniquePeerID.text = str(multiplayer.get_unique_id())

func add_player_character(peer_id):
	connected_peer_ids.append(peer_id)
	var player_character = preload("res://player_character/player_character.tscn").instantiate()
	player_character.set_multiplayer_authority(peer_id)
	add_child(player_character)
	if peer_id == multiplayer.get_unique_id():
		local_player_character = player_character

@rpc	
func add_newly_connected_player_character(new_peer_id):
	add_player_character(new_peer_id)
	
@rpc
func add_previously_connected_player_characters(peer_ids):
	for peer_id in peer_ids:
		add_player_character(peer_id)

func _on_message_input_text_submitted(new_text):
	local_player_character.rpc("display_message", new_text)
	$MessageInput.text = ""
	$MessageInput.release_focus()

Can someone please explain to me what am I doing wrong?
Thanks in advance.

I think this is intended behavior. Is your client trying to connect from another computer or network?

TRAILtheGREAT | 2023-04-29 17:26

same network, different computer

|3enchmark | 2023-04-29 20:52

Are you absolutely sure you’re using the correct ip address? Are you sure you don’t have any firewall rules in place that might be blocking your application?

TRAILtheGREAT | 2023-04-29 22:48

I am certain i did add an exception to firewall (ESET → Advanced → Network → Firewall → selected my app, TCP/UDP) and I just double checked the ip address (cmd → ipconfig → copy-paste and hardcode it into the script)

|3enchmark | 2023-04-29 23:39