Search Unity

Question NetworkList (Netcode for gameObjects)

Discussion in 'Documentation' started by abbouhya, Feb 10, 2023.

  1. abbouhya

    abbouhya

    Joined:
    Feb 1, 2023
    Posts:
    1
    Hello,

    I've been trying to learn unity netcode lately, and I find it very easy and fascinating.
    And I have a small question for people with more experience with it.

    I'm trying to find a way to create a list of player class objects to store specific data about every player on my game as follows:

    private NetworkList<Player> playerList;

    void awake(){
    playerList = new NetworkList<Player>();
    }

    However, I keep getting the error : The type 'Player' must be a non-nullable value type.

    While reading through unity documentation I found that they used as an example a very similar code:

    private NetworkList<AreaWeaponBooster> TeamAreaWeaponBoosters;

    void Awake()
    {
    TeamAreaWeaponBoosters = new NetworkList<AreaWeaponBooster>();
    }

    here's the link of the documentation :
    https://docs-multiplayer.unity3d.com/netcode/current/basics/networkvariable


    So I can't seem to understand how is my code any different. Can I make the list somehow non nullable? otherwise, is there an easy way to communicate specefic data to each player like hp, mana etc.. without getting lost in too much indexing.


    Thanks!
     
  2. Sean2212

    Sean2212

    Joined:
    Jun 9, 2017
    Posts:
    12
    Make sure the type you're using in the NetworkList is a struct (not a class). AreaWeaponBooster is a struct.
     
  3. LavarBal

    LavarBal

    Joined:
    Aug 20, 2019
    Posts:
    4
    Im trying to do the same thing as abbouhya and get the same problem using a struct

    public struct Player:INetworkSerializable
    {
    public ulong id;
    public string name;
    public float health;
    public int kills;
    public int deaths;
    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
    if (serializer.IsReader)
    {
    // The complex type handles its own de-serialization

    // Now de-serialize the non-complex type properties
    var reader = serializer.GetFastBufferReader();
    reader.ReadValueSafe(out id);
    reader.ReadValueSafe(out name);
    reader.ReadValueSafe(out health);
    reader.ReadValueSafe(out kills);
    reader.ReadValueSafe(out deaths);
    }
    else
    {
    // The complex type handles its own serialization

    // Now serialize the non-complex type properties
    var writer = serializer.GetFastBufferWriter();
    writer.WriteValueSafe(id);
    writer.WriteValueSafe(name);
    writer.WriteValueSafe(health);
    writer.WriteValueSafe(kills);
    writer.WriteValueSafe(deaths);
    }
    }
    }
    NetworkList<Player> playersList;

    >Player must be non-nullable
     
  4. deeffoora

    deeffoora

    Joined:
    Jun 28, 2018
    Posts:
    4
    Last edited: Oct 26, 2023
    trombonaut likes this.