com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

Can't compile INetworkSerializable of custom struct array

Open akoolenbourke opened this issue 3 years ago • 3 comments

Description

I have a custom struct that is INetworkSerializable. That struct has an array of another struct that is INetworkSerializable. This won't compile and I get the error

The type 'Thing' 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 'NetworkVariable<T>'

Reproduce Steps

  1. Create a struct with INetworkSerializable
 public struct Bob : INetworkSerializable
    {
        public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
        {
        }
    }

  1. Create a second INetworkSerializable class with an array of the first
public struct Thing : INetworkSerializable
    {
        public Bob[] targets;

        public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
        {
        }
    }
  1. Add a NetworkVariable of this NetworkVariable<Thing> thing; r

Actual Outcome

Compiler error:

The type 'Thing' 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 'NetworkVariable<T>'

Expected Outcome

It works and I can serialise these things.

Environment

  • OS: Windows 10
  • Unity Version: 2021.3.11f.32
  • Netcode Version: 1.0.0-pre.10 (Same issue with pre9)

Additional Context

The docs suggest I can do things like int[] as well but those don't work either. https://docs-multiplayer.unity3d.com/netcode/current/advanced-topics/serialization/inetworkserializable/index.html

Arrays are not non-nullable it seems so this just dies.

akoolenbourke avatar Jun 28 '22 19:06 akoolenbourke

It's known that NetworkVariable doesn't support anything that has a managed type within it. The docs are unfortunately out of date on this - we don't support serializing arrays as part of network variables. Instead, you can use NativeArray<Bob> (if your array is going to be of a static size) or NativeList<Bob> (if your array needs to be dynamically sized).

You'll then have to serialize each item in the list or array individually within your NetworkSerialize method.

ShadauxCat avatar Jul 06 '22 19:07 ShadauxCat

Backlog MTT-4079

ashwinimurt avatar Jul 12 '22 23:07 ashwinimurt

Assigning to myself to close when the Docs are updated.

ashwinimurt avatar Aug 01 '22 20:08 ashwinimurt

Hello. This is no longer required as 1.1.0 supports the docs as written. Thanks for reporting the issue

mtarzwell avatar May 15 '23 14:05 mtarzwell