Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Bug 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:
    2
    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.