Recursive Nested Serialization does not work with NetworkVariables "non-nullable" error.

I get an error must be a non-nullable type when using nested serialization with network variables.

Expected:
Nested serialization works per the doc: https://docs-multiplayer.unity3d.com/docs/develop/advanced-topics/serialization/inetworkserializable

//error about AbilityParam must be non-nullable type
[HideInInspector] private NetworkVariable<AbilityParam> m_CurrentParams = new NetworkVariable<AbilityParam>();

public struct AbilityParam: INetworkSerializable, IEquatable<AbilityParam>
    {
        public AbilityTargetParam[] m_Targets;
        public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
        {
          
            int length = 0;
            if(!serializer.IsReader)
            {
                length = m_Targets.Length;
            }
            serializer.SerializeValue(ref length);

            if(serializer.IsReader)
            {
                m_Targets = new AbilityTargetParam[length];
            }

            for (int i = 0; i < length; ++i)
            {
                m_Targets[i].NetworkSerialize(serializer);
            }
        }

        public bool Equals(AbilityParam other)
        {
            return m_Targets.Equals(other.m_Targets);
        }
    }

    public struct AbilityTargetParam: INetworkSerializable, IEquatable<AbilityTargetParam>
    {
        public NetworkedActor Target
        {
            get
            {
                m_Target.TryGet(out NetworkedActor net);
                return net;
            }
        }
        public NetworkBehaviourReference m_Target; //Network actor
        public Vector3 m_TargetPosition;
        public Vector3 m_Origin;
        public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
        {
serializer.SerializeValue(ref m_Target);
            serializer.SerializeValue(ref m_TargetPosition);
            serializer.SerializeValue(ref m_Origin);
        }
        public bool Equals(AbilityTargetParam other)
        {
            return m_Target.Equals(other.m_Target) && m_TargetPosition.Equals(other.m_TargetPosition) && m_Origin.Equals(other.m_Origin);
        }
    }
1 Like

It seams like a larger bug after more investigation. It seams having an array in a struct triggers a null-able error and won’t serialize with network variables. While arrays are nullable the doc clearly states they can be serialized.

1 Like

I am facing the same problem.
Array and Recursive Nested Serialization sample code in document does not work with 1.0.0-pre.4.

2 Likes

Bumping this thread. I also created another issue: https://answers.unity.com/questions/1889677/how-to-networkserialize-an-array-always-getting-no.html

Im not sure how anyone is supposed to use this networking framework without the ability to sync even the most basic data structures.

1 Like

same issue, i look forward to unity ignoring the issue as usual

1 Like

Still an issue for me, been following the documentation and attempting to use NativeArray and C# Arrays in a NetworkList. unfortunately, they are nullable and can’t be used. So how is one meant to serialize and sync array structures?

I found this thread through a google search and just realized it’s kinda old. But I figured out a way to sync INetworkSerializable structures with lists by following these directions in the documentation:

So maybe if other people find this thread with the same question, this will help you. Another tip is that the type that I’m storing in the list in memory is actually different than the type that I serialize and deserialize. I wrote thin DTO structures that can be translated to the core structures so that I don’t have to worry about keeping all my core structures implementing INetworkSerializable.

1 Like