2

i'm trying to serialize this structs for sending it via Unity NetworkList. Probably i'm doing the wrong serialization but idk what, unity documentation: https://docs-multiplayer.unity3d.com/docs/advanced-topics/serialization/inetworkserializable

public struct PlayerPositionData : INetworkSerializable, IEquatable<PlayerPositionData>
{
    public int numeroAncora;
    public Vector3[] posizioni;
    public Quaternion[] rotazioni;
 
    public PlayerPositionData(int numeroAnc, Vector3[] posiz, Quaternion[] rotaz)
    {
        numeroAncora = numeroAnc;
        posizioni = posiz;
        rotazioni = rotaz;
    }
 
 
    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
        serializer.SerializeValue(ref numeroAncora);
 
        //SERIALIZZO ARRAY POSIZIONI
        int length = 0;
        if (!serializer.IsReader)
        {
            length = posizioni.Length;
        }
 
        serializer.SerializeValue(ref length);
 
        // Array
        if (serializer.IsReader)
        {
            posizioni = new Vector3[length];
        }
 
        for (int n = 0; n < length; ++n)
        {
            serializer.SerializeValue(ref posizioni[n]);
        }
 
 
     
        //SERIALIZZO ARRAY ROTAZIONI
        if (!serializer.IsReader)
        {
            length = rotazioni.Length;
        }
 
        serializer.SerializeValue(ref length);
 
        // Array
        if (serializer.IsReader)
        {
            rotazioni = new Quaternion[length];
        }
 
        for (int n = 0; n < length; ++n)
        {
            serializer.SerializeValue(ref rotazioni[n]);
        }
    }
 
    public bool Equals(PlayerPositionData other)
    {
        return posizioni.Length == other.posizioni.Length && numeroAncora == other.numeroAncora;
    }
}

and this:

public struct PlayerData : INetworkSerializable, IEquatable<PlayerData>
{
    public ulong idRete;
    public int numeroPlayer;
    public PlayerPositionData posizioni;
 
    public PlayerData(ulong idRet, int payerN, PlayerPositionData posiz)
    {
        idRete = idRet;
        numeroPlayer = payerN;
        posizioni = posiz;
    }
 
 
    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
        serializer.SerializeValue(ref idRete);
        serializer.SerializeValue(ref numeroPlayer);
 
        posizioni.NetworkSerialize(serializer);
    }
 
    public bool Equals(PlayerData other)
    {
        return idRete == other.idRete;
    }
}

in this way:

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? the PlayerData struct work without nested PlayerPositionData...

Alessandro Zago
  • 793
  • 3
  • 12
  • 33
  • `rotazioni` and `posizioni` are arrays, which are reference types not value types, so that's probably why. See [this answer](https://stackoverflow.com/a/1533770/1092820) for more info. – Ruzihm Feb 09 '22 at 20:52
  • I haven't tried this but maybe using something in `Unity.Collections` namespace could help. For instance, [here is `NativeList`, a resizeable list](https://docs.unity3d.com/Packages/com.unity.collections@1.1/api/Unity.Collections.NativeList-1.html). Just be sure to [manage](https://docs.unity3d.com/Packages/com.unity.collections@1.1/manual/allocation.html) this memory, as it us unmanaged, and the garbage collector does not know about it. – Ruzihm Feb 09 '22 at 20:56
  • 1
    @Ruzihm strange though because the linked doc itself has one example using `int[]` and I don't really see the difference to what OP is doing O.o ... on the other hand of course it isn't used in a `NetworkList` there ... – derHugo Feb 10 '22 at 06:29
  • 1
    @OP have you considered to simply not use a `NetworkList` but yet another custom `INetworkSerializable` containing the `PlayerData[]` and rather synchronize it manually? – derHugo Feb 10 '22 at 06:32
  • Also [`Vector3` etc of course are built-in supported](https://docs-multiplayer.unity3d.com/docs/advanced-topics/serialization/arrays) ... – derHugo Feb 10 '22 at 08:00
  • 2
    Or have you also considered using classes and [custom serialization](https://docs-multiplayer.unity3d.com/docs/advanced-topics/custom-serialization/index.html) ? – derHugo Feb 10 '22 at 08:12

0 Answers0