Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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

Discussion in 'Netcode for GameObjects' started by hoesterey, Jan 20, 2022.

  1. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    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


    Code (CSharp):
    1.  
    2. //error about AbilityParam must be non-nullable type
    3. [HideInInspector] private NetworkVariable<AbilityParam> m_CurrentParams = new NetworkVariable<AbilityParam>();
    4.  
    5. public struct AbilityParam: INetworkSerializable, IEquatable<AbilityParam>
    6.     {
    7.         public AbilityTargetParam[] m_Targets;
    8.         public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    9.         {
    10.          
    11.             int length = 0;
    12.             if(!serializer.IsReader)
    13.             {
    14.                 length = m_Targets.Length;
    15.             }
    16.             serializer.SerializeValue(ref length);
    17.  
    18.             if(serializer.IsReader)
    19.             {
    20.                 m_Targets = new AbilityTargetParam[length];
    21.             }
    22.  
    23.             for (int i = 0; i < length; ++i)
    24.             {
    25.                 m_Targets[i].NetworkSerialize(serializer);
    26.             }
    27.         }
    28.  
    29.         public bool Equals(AbilityParam other)
    30.         {
    31.             return m_Targets.Equals(other.m_Targets);
    32.         }
    33.     }
    34.  
    35.     public struct AbilityTargetParam: INetworkSerializable, IEquatable<AbilityTargetParam>
    36.     {
    37.         public NetworkedActor Target
    38.         {
    39.             get
    40.             {
    41.                 m_Target.TryGet(out NetworkedActor net);
    42.                 return net;
    43.             }
    44.         }
    45.         public NetworkBehaviourReference m_Target; //Network actor
    46.         public Vector3 m_TargetPosition;
    47.         public Vector3 m_Origin;
    48.         public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    49.         {
    50. serializer.SerializeValue(ref m_Target);
    51.             serializer.SerializeValue(ref m_TargetPosition);
    52.             serializer.SerializeValue(ref m_Origin);
    53.         }
    54.         public bool Equals(AbilityTargetParam other)
    55.         {
    56.             return m_Target.Equals(other.m_Target) && m_TargetPosition.Equals(other.m_TargetPosition) && m_Origin.Equals(other.m_Origin);
    57.         }
    58.     }
     
    modaka-cov likes this.
  2. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    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.
     
    jc2 likes this.
  3. modaka-cov

    modaka-cov

    Joined:
    Jul 5, 2021
    Posts:
    3
    I am facing the same problem.
    Array and Recursive Nested Serialization sample code in document does not work with 1.0.0-pre.4.
     
    jc2 and hoesterey like this.
  4. Louiebloo

    Louiebloo

    Joined:
    Jan 26, 2022
    Posts:
    4
    jc2 likes this.
  5. jc2

    jc2

    Joined:
    Jul 14, 2015
    Posts:
    15
    same issue, i look forward to unity ignoring the issue as usual
     
    hoesterey likes this.
  6. MintRogue

    MintRogue

    Joined:
    Jan 29, 2017
    Posts:
    2
    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?
     
  7. MintRogue

    MintRogue

    Joined:
    Jan 29, 2017
    Posts:
    2
    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:

    https://docs-multiplayer.unity3d.co...x.html#custom-networkvariable-implementations

    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.
     
    RikuTheFuffs likes this.