Search Unity

Question How can I create a NetworkList/NetworkVariable<List> of lists?

Discussion in 'Multiplayer' started by LongAndHappy, Jul 2, 2022.

  1. LongAndHappy

    LongAndHappy

    Joined:
    May 10, 2019
    Posts:
    6
    I'm trying to create a game where players take turns drawing to their screens. I need a list of Vector2 points (for each paint stroke), and then I need a list of these strokes to show a full drawing.

    Traditionally, I would create a List<List<Vector2>> but since I want this information stored on and shared from my server, I need a NetworkVariable or NetworkList to keep track of everyone's paint strokes.

    _________________________________________________
    Attempt 1
    My code initializing my NetworkList of lists:
    Code (CSharp):
    1. public NetworkList<List<Vector2>> playerStrokes = new NetworkList<List<Vector2>>(default, NetworkVariableBase.DefaultReadPerm, NetworkVariableWritePermission.Owner);
    This code gives the following error:
    error CS8377: The type 'List<Vector2>' 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>'

    This error doesn't make sense because Lists are non-nullable by default, and Vector2's are structs.

    _________________________________________________
    Attempt 2
    I've also tried creating an INetworkSerializable of Vector2[], but I'm getting the same error..
    Code (CSharp):
    1. [System.Serializable]
    2. public struct NetworkListOfStrokes : INetworkSerializable
    3. {
    4.     public int playerIndex;
    5.     public Vector2[] strokes;
    6.  
    7.     public NetworkListOfStrokes(int _playerindex, Vector2[] _strokes)
    8.     {
    9.         playerIndex = _playerindex;
    10.         strokes= _strokes;
    11.     }
    12.  
    13.     public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    14.     {
    15.         serializer.SerializeValue(ref playerIndex);
    16.         serializer.SerializeValue(ref strokes);
    17.     }
    18. }
    Code (CSharp):
    1. public NetworkList<NetworkListOfStrokes> playerStrokes2 = new NetworkList<NetworkListOfStrokes>(default, NetworkVariableBase.DefaultReadPerm, NetworkVariableWritePermission.Owner);
    and the error is
    error CS8377: The type 'NetworkListOfStrokes' 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>'


    It seems netcode really just doesn't want me to have nested lists. Has anybody been able to get a list of lists working in netcode? I'll update this post if I find the solution.
     
    Floofyhair_ likes this.
  2. Diamondemon

    Diamondemon

    Joined:
    Dec 19, 2021
    Posts:
    1