TLDR
Client and transport ids are getting mixed up. NetworkTransport.ServerClientId should be a transport id and NetworkManager.LocalClientId is a client id yet the current implementation uses them interchangebly.
Detailed description with context
I am currently building a custom transport and I ran into some problems regarding client ids. My transport does assign and use custom ids internally (i.e. transport ids) and in my current implementation the transport id for the server is not 0 but 2.
So naturally I used the following implementation in my implementation of NetworkTransport:
public override ulong ServerClientId => 2;
This implementation works fine and the messages are routed correctly to the server. However, I run into problems if a player is hosting a game. When the player prefab for the host is spawned it receives an incorrect owner id (always 0).
In particular the problem seems to be that the ServerClientId is hardcoded to 0 in NetworkManager:
|
public const ulong ServerClientId = 0; |
and then used in several places e.g. when passing the ownerClientId:
|
HandleConnectionApproval(ServerClientId, response); |
on the other hand, NetworkObject uses NetworkManager.LocalClientId when comparing for ownership:
|
public bool IsOwner => NetworkManager != null && OwnerClientId == NetworkManager.LocalClientId; |
and NetworkManager.LocalClientId directly references the ServerClientId implemented by the transport:
|
public ulong LocalClientId |
|
{ |
|
get => IsServer ? NetworkConfig.NetworkTransport.ServerClientId : m_LocalClientId; |
|
internal set => m_LocalClientId = value; |
So as far as I can tell, if the transport implementation returns another value than 0 for ServerClientId on the server side there will be an ownership issue with the player prefab for the host.
I think the real issue however is that currently client and transport ids are getting mixed up. NetworkTransport.ServerClientId is a transport id and NetworkManager.LocalClientId is a client id yet the currently implementation uses them interchangebly.
Btw. the workaround is to return a different ServerClientId on the server/client side in the implementation of the transport:
public override ulong ServerClientId => isHost ? 0 : serverTransportId;
OS: Win10
Netcode Version: 1.2.0
Unity Version: 2020.3.41f1
TLDR
Client and transport ids are getting mixed up.
NetworkTransport.ServerClientIdshould be a transport id andNetworkManager.LocalClientIdis a client id yet the current implementation uses them interchangebly.Detailed description with context
I am currently building a custom transport and I ran into some problems regarding client ids. My transport does assign and use custom ids internally (i.e. transport ids) and in my current implementation the transport id for the server is not
0but2.So naturally I used the following implementation in my implementation of
NetworkTransport:This implementation works fine and the messages are routed correctly to the server. However, I run into problems if a player is hosting a game. When the player prefab for the host is spawned it receives an incorrect owner id (always
0).In particular the problem seems to be that the
ServerClientIdis hardcoded to0inNetworkManager:com.unity.netcode.gameobjects/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Line 274 in 3d2c266
and then used in several places e.g. when passing the
ownerClientId:com.unity.netcode.gameobjects/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Line 946 in 3d2c266
on the other hand,
NetworkObjectusesNetworkManager.LocalClientIdwhen comparing for ownership:com.unity.netcode.gameobjects/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Line 86 in 3d2c266
and
NetworkManager.LocalClientIddirectly references theServerClientIdimplemented by the transport:com.unity.netcode.gameobjects/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Lines 284 to 287 in 3d2c266
So as far as I can tell, if the transport implementation returns another value than
0forServerClientIdon the server side there will be an ownership issue with the player prefab for the host.I think the real issue however is that currently client and transport ids are getting mixed up.
NetworkTransport.ServerClientIdis a transport id andNetworkManager.LocalClientIdis a client id yet the currently implementation uses them interchangebly.Btw. the workaround is to return a different
ServerClientIdon the server/client side in the implementation of the transport:OS: Win10
Netcode Version: 1.2.0
Unity Version: 2020.3.41f1