question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NetworkList<T> initialization and Docs: /versioned_docs/version-1.0.0/advanced-topics/serialization/inetworkserializable.md

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
davidyrgilbertcommented, Nov 19, 2021

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.

 public struct RemoteFileSourceInformation : INetworkSerializable, IEquatable<RemoteFileSourceInformation>
    {
        public int SourceId;
        public FixedString32Bytes FilePath;
        public FixedString32Bytes SourceName;

        public RemoteFileSourceInformation(int sourceId, FixedString32Bytes filePath, FixedString32Bytes sourceName)
        {
            SourceId = sourceId;
            FilePath = filePath;
            SourceName = sourceName;
        }
        
        // INetworkSerializable
        public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
        {
            serializer.SerializeValue(ref SourceId);
            serializer.SerializeValue(ref FilePath);
            serializer.SerializeValue(ref SourceName);
        }
        // ~INetworkSerializable
        
        // IEquatable
        public bool Equals(RemoteFileSourceInformation other)
        {
            return SourceId == other.SourceId && FilePath.Equals(other.FilePath) && SourceName.Equals(other.SourceName);
        }

        // public override bool Equals(object obj)
        // {
        //     return obj is RemoteFileSourceInformation other && Equals(other);
        // }
        //
        // public override int GetHashCode()
        // {
        //     return HashCode.Combine(SourceId, FilePath, SourceName);
        // }
        
        // ~IEquatable
    }

If that’s the correct way to do it, this should be in the docs too.

1reaction
LukeStampflicommented, Nov 19, 2021

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found