Can't compile INetworkSerializable of custom struct array
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
- Create a struct with INetworkSerializable
public struct Bob : INetworkSerializable
{
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
}
}
- 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
{
}
}
- 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.
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.
Backlog MTT-4079
Assigning to myself to close when the Docs are updated.
Hello. This is no longer required as 1.1.0 supports the docs as written. Thanks for reporting the issue