Search Unity

Question Serialize Array of Vector3 and Quaternion

Discussion in 'Netcode for GameObjects' started by HRDev, Feb 9, 2022.

  1. HRDev

    HRDev

    Joined:
    Jun 4, 2018
    Posts:
    58
    Hello, i'm trying to serialize this structs:


    Code (CSharp):
    1. public struct PlayerPositionData : INetworkSerializable, IEquatable<PlayerPositionData>
    2. {
    3.     public int numeroAncora;
    4.     public Vector3[] posizioni;
    5.     public Quaternion[] rotazioni;
    6.  
    7.     public PlayerPositionData(int numeroAnc, Vector3[] posiz, Quaternion[] rotaz)
    8.     {
    9.         numeroAncora = numeroAnc;
    10.         posizioni = posiz;
    11.         rotazioni = rotaz;
    12.     }
    13.  
    14.  
    15.     public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    16.     {
    17.         serializer.SerializeValue(ref numeroAncora);
    18.  
    19.         //SERIALIZZO ARRAY POSIZIONI
    20.         int length = 0;
    21.         if (!serializer.IsReader)
    22.         {
    23.             length = posizioni.Length;
    24.         }
    25.  
    26.         serializer.SerializeValue(ref length);
    27.  
    28.         // Array
    29.         if (serializer.IsReader)
    30.         {
    31.             posizioni = new Vector3[length];
    32.         }
    33.  
    34.         for (int n = 0; n < length; ++n)
    35.         {
    36.             serializer.SerializeValue(ref posizioni[n]);
    37.         }
    38.  
    39.  
    40.      
    41.         //SERIALIZZO ARRAY ROTAZIONI
    42.         if (!serializer.IsReader)
    43.         {
    44.             length = rotazioni.Length;
    45.         }
    46.  
    47.         serializer.SerializeValue(ref length);
    48.  
    49.         // Array
    50.         if (serializer.IsReader)
    51.         {
    52.             rotazioni = new Quaternion[length];
    53.         }
    54.  
    55.         for (int n = 0; n < length; ++n)
    56.         {
    57.             serializer.SerializeValue(ref rotazioni[n]);
    58.         }
    59.     }
    60.  
    61.     public bool Equals(PlayerPositionData other)
    62.     {
    63.         return posizioni.Length == other.posizioni.Length && numeroAncora == other.numeroAncora;
    64.     }
    65. }
    Code (CSharp):
    1. public struct PlayerData : INetworkSerializable, IEquatable<PlayerData>
    2. {
    3.     public ulong idRete;
    4.     public int numeroPlayer;
    5.     public PlayerPositionData posizioni;
    6.  
    7.     public PlayerData(ulong idRet, int payerN, PlayerPositionData posiz)
    8.     {
    9.         idRete = idRet;
    10.         numeroPlayer = payerN;
    11.         posizioni = posiz;
    12.     }
    13.  
    14.  
    15.     public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    16.     {
    17.         serializer.SerializeValue(ref idRete);
    18.         serializer.SerializeValue(ref numeroPlayer);
    19.  
    20.         posizioni.NetworkSerialize(serializer);
    21.     }
    22.  
    23.     public bool Equals(PlayerData other)
    24.     {
    25.         return idRete == other.idRete;
    26.     }
    27. }

    in this way:
    Code (CSharp):
    1. private NetworkList<PlayerData> datiPlayers = new NetworkList<PlayerData>();
    but i recive this error:
    error CS8377: The type 'PlayerData' 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 'NetworkList<T>'


    why?
     
    jc2 likes this.
  2. jc2

    jc2

    Joined:
    Jul 14, 2015
    Posts:
    15
    why do you keep ignoring these threads unity
     
    HRDev likes this.
  3. Brian-Kryptomon

    Brian-Kryptomon

    Joined:
    Apr 17, 2022
    Posts:
    17
    Bump - I'm stuck in the same place.

    @luke-unity Any ideas here?
     
  4. Brian-Kryptomon

    Brian-Kryptomon

    Joined:
    Apr 17, 2022
    Posts:
    17
    Apologies - please disregard. The moment I submitted it I figured out that I hadn't converted one of my classes to a struct. :(