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

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> 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:

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…

[System.Serializable]
public struct NetworkListOfStrokes : INetworkSerializable
{
    public int playerIndex;
    public Vector2[] strokes;

    public NetworkListOfStrokes(int _playerindex, Vector2[] _strokes)
    {
        playerIndex = _playerindex;
        strokes= _strokes;
    }

    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
        serializer.SerializeValue(ref playerIndex);
        serializer.SerializeValue(ref strokes);
    }
}
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.

1 Like

Hello!
I found something close to a solution in the documentation, see there if it helps you:

Basically, you have to create your own type of NetworkVariable.

Hope this helps :slight_smile: