Inconsistent use of client/transport ids
See original GitHub issueTLDR
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
:
https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/3d2c266a4dab4c1cb851479bd1b39f602777278a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs#L274
and then used in several places e.g. when passing the ownerClientId
:
https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/3d2c266a4dab4c1cb851479bd1b39f602777278a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs#L946
on the other hand, NetworkObject
uses NetworkManager.LocalClientId
when comparing for ownership:
https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/3d2c266a4dab4c1cb851479bd1b39f602777278a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs#L86
and NetworkManager.LocalClientId
directly references the ServerClientId
implemented by the transport:
https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/3d2c266a4dab4c1cb851479bd1b39f602777278a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs#L284-L287
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
Issue Analytics
- State:
- Created 8 months ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
@FreshlyBrewedCode That does indeed look like it is returning the incorrect value. Let me double check as to why that is not returning the NGO layer client identifier, and if it is a mistake/bug I will get a PR up to fix that issue.
Thank you for pointing this out.
MTT-5387