NetworkList<T> initialization and Docs: /versioned_docs/version-1.0.0/advanced-topics/serialization/inetworkserializable.md
See original GitHub issueI can not initialize NetworkList.
using Unity.Netcode;
public struct LobbyPlayerState : INetworkSerializable
{
public ulong ClientId;
public string PlayerName;
public bool IsReady;
public LobbyPlayerState(ulong clientId, string playerName, bool isReady)
{
ClientId = clientId;
PlayerName = playerName;
IsReady = isReady;
}
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref ClientId);
serializer.SerializeValue(ref PlayerName);
serializer.SerializeValue(ref IsReady);
}
}
Next I try initialize NetworkList type of LobbyPlayerState:
public class Lobby : NetworkBehaviour
{
private NetworkList<LobbyPlayerState> lobbyPlayers = new NetworkList<LobbyPlayerState>();
}
And get error:
Severity Code Description Project File Line Suppression State Error CS8377 The type ‘LobbyPlayerState’ must be a non-nullable value type, along with all fields at any level of nesting, in order to use it as parameter ‘T’ in the generic type or method ‘NetworkList<T>’
How can I fix this error and initialize the NetworkList for further use? The instructions lack a more detailed disclosure of this point.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Had the same issue, I think also implementing IEquatable seems to fix that error, or at least makes it not show up anymore. Have not actually tested the functionality.
If that’s the correct way to do it, this should be in the docs too.
Yes implemententing IEquatable is necessary for NetworkList and your implementation looks correct. You currently have the GetHashCode and Equals function commented out, I’d recommend to override them like you did instead of using the default struct equality functions.
As for adding this to the docs, could you open an issue in the docs repo?