godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.14k stars 21.19k forks source link

Peer connected signal does not fire on clients, only on host #87158

Closed jruiz94 closed 10 months ago

jruiz94 commented 10 months ago

Tested versions

System information

Godot v4.2.stable - Windows 10.0.22631 - Vulkan (Forward+) - dedicated NVIDIA GeForce GTX 1070 Ti (NVIDIA; 31.0.15.4633) - Intel(R) Core(TM) i5-8600K CPU @ 3.60GHz (6 Threads)

Issue description

When creating a multiplayer game with ENetMultiplayerPeer the peer_connected signal (and also peer_disconnected) is only received by the host, not the other peers. The docs say that every other peer receives this signal not only the host: https://docs.godotengine.org/en/stable/tutorials/networking/high_level_multiplayer.html#managing-connections

Steps to reproduce

Create a simple Node Attach the following script

extends Node

var network: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
var max_players: int = 4

func _process(delta):
    if Input.is_action_just_pressed("ui_left"):
        start_server(54545)
    if Input.is_action_just_pressed("ui_right"):
        connect_to_server("127.0.0.1", 54545)

func start_server(port: int) -> String:
    network.create_server(port, max_players)
    multiplayer.multiplayer_peer = network
    multiplayer.multiplayer_peer.peer_connected.connect(_peer_connected)
    multiplayer.multiplayer_peer.peer_disconnected.connect(_peer_disconnected)
    print("I am " + str(get_tree().get_multiplayer().get_unique_id()) + ". Server started")
    return ""

func connect_to_server(ip: String, port: int) -> String:
    network.create_client(ip, port)
    multiplayer.multiplayer_peer = network
    while multiplayer.multiplayer_peer.get_connection_status() == MultiplayerPeer.CONNECTION_CONNECTING:
        await get_tree().create_timer(0.01).timeout
    if multiplayer.multiplayer_peer.get_connection_status() == MultiplayerPeer.CONNECTION_DISCONNECTED:
        print("Failed to start multiplayer client.")
        return "Error while starting multiplayer client"
    multiplayer.multiplayer_peer.peer_connected.connect(_peer_connected)
    multiplayer.multiplayer_peer.peer_disconnected.connect(_peer_disconnected)
    print("I am " + str(get_tree().get_multiplayer().get_unique_id()) + ". Connected to server successfully")
    return ""

func _peer_connected(player_id):
    print("I am " + str(get_tree().get_multiplayer().get_unique_id()) + ". Player " + str(player_id) + " connected")

func _peer_disconnected(player_id):
    print("I am " + str(get_tree().get_multiplayer().get_unique_id()) + "Player " + str(player_id) + " disconnected")

Run in debug mode, launch 3 instances (Debug menu on top > Run Multiple Instances > Launch 3 instances (minimum)) First instance -> Press left arrow. This will be the host. Check logs Second instance -> Press right arrow. This will be first client. Check logs. Server receives peer_connected signal and logs it Third instance -> Press right arrow. This will be second client. Check logs. Server receives peer_connected signal and logs it, however the first client doesn't receive anything

Minimal reproduction project (MRP)

bugsignalpeer.zip

jruiz94 commented 10 months ago

My bad, I was connecting to the signal emitted by ENetMultiplayerPeer and not to the signal emitted by MultiplayerAPI It's a little bit confusing that the name is the same for both IMO. So it's not a bug. This is the fixed script:

extends Node

var network: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
var max_players: int = 4

func _process(delta):
    if Input.is_action_just_pressed("ui_left"):
        start_server(54545)
    if Input.is_action_just_pressed("ui_right"):
        connect_to_server("127.0.0.1", 54545)

func start_server(port: int) -> String:
    network.create_server(port, max_players)
    multiplayer.multiplayer_peer = network
    multiplayer.peer_connected.connect(_peer_connected) # This line changed
    multiplayer.peer_disconnected.connect(_peer_disconnected) # This line changed
    print("I am " + str(get_tree().get_multiplayer().get_unique_id()) + ". Server started")
    return ""

func connect_to_server(ip: String, port: int) -> String:
    network.create_client(ip, port)
    multiplayer.multiplayer_peer = network
    while multiplayer.multiplayer_peer.get_connection_status() == MultiplayerPeer.CONNECTION_CONNECTING:
        await get_tree().create_timer(0.01).timeout
    if multiplayer.multiplayer_peer.get_connection_status() == MultiplayerPeer.CONNECTION_DISCONNECTED:
        print("Failed to start multiplayer client.")
        return "Error while starting multiplayer client"
    multiplayer.peer_connected.connect(_peer_connected) # This line changed
    multiplayer.peer_disconnected.connect(_peer_disconnected) # This line changed
    print("I am " + str(get_tree().get_multiplayer().get_unique_id()) + ". Connected to server successfully")
    return ""

func _peer_connected(player_id):
    print("I am " + str(get_tree().get_multiplayer().get_unique_id()) + ". Player " + str(player_id) + " connected")

func _peer_disconnected(player_id):
    print("I am " + str(get_tree().get_multiplayer().get_unique_id()) + "Player " + str(player_id) + " disconnected")

Sorry for the inconvenience hopefully this helps someone