+1 vote

Here is my code

extends Node


const DEFAULT_IP := "127.0.0.1"
const DEFAULT_PORT := 54321
const MAX_PLAYERS := 6

var players := {}
var game: Node2D


func create_server() -> void:
    var net = NetworkedMultiplayerENet.new()
    net.create_server(DEFAULT_PORT, MAX_PLAYERS)
    get_tree().network_peer = net
    get_tree().connect("network_peer_connected", self, "player_connected")
    get_tree().connect("network_peer_disconnected", self, "player_disconnected")
    print("Server created.")


func join_server() -> void:
    var net = NetworkedMultiplayerENet.new()
    net.create_client(DEFAULT_IP, DEFAULT_PORT)
    get_tree().network_peer = net
    print("Server joined.")


func player_connected(id: int) -> void:
    rpc("register_player", id)


func player_disconnected(id: int) -> void:
    game.remove_player(id)


remote func register_player(id: int) -> void:
    game.create_player(id)
Godot version 3.4.2
in Engine by (8,528 points)

2 Answers

+1 vote
Best answer

Nevermind I'm dumb

I forgot to call the function that changes the id of the player to something besides the default 1

by (8,528 points)
0 votes

I don't know much about networking (I actually just started today) but is it possible that you are looking for the unique ID instead? that can be found with get_tree().get_network_unique_id(). Hope this helps.

by (113 points)

networkpeerconnected ( int id )

Emitted whenever this SceneTree's network_peer connects with a new peer. ID is the peer ID of the new peer. Clients get notified when other clients connect to the same server. Upon connecting to a server, a client also receives this signal for the server (with ID being 1).

The docs say that it should be the ID of the peer, so the unique id.

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.