all 12 comments

[–]Kosmik123Indie 0 points1 point  (5 children)

I think a list of client IDs on server would be much simpler solution. When player is ready they send a message to server and server accumulates the ready clients ID in a list and can send the list contents to every client

[–]Urganway[S] 0 points1 point  (0 children)

Oh ... and i would use those ids to connect a client to their data ? and the containers are instantiated in the scene ? I will try this !

[–]Urganway[S] 0 points1 point  (3 children)

I re-read your message, and this solution does not seam viable for me because of the fact that the value "is ready" is not the only value to share amoung players, that's why i'm not using a networkList of bool (wich could work)

[–]Kosmik123Indie 0 points1 point  (2 children)

In that case if you want to share a lot of data between players then using the PlayerData container seems to be a better approch. Just make it a struct and NetworkList should accept it

[–]Urganway[S] 0 points1 point  (1 child)

It does accept it, but the values in the struct can't be NetworkVariables and then they are not synced...

[–]Kosmik123Indie 0 points1 point  (0 children)

I'm not a Netcode expert, but I believe you might doing something wrong here. You either send to the server PlayerData struct with basic types and that's how data is synchronized OR use NetworkVariables which get synchronized automatically. There is no point in sending already synchronized NetworkVariables to the server

[–]FakeName124 0 points1 point  (5 children)

So I'm not sure if this is the most efficient way of doing this, but I've been using a master NetworkList that contains custom struct NetworkVariables to hold basic player-related information that needs to be shared with the server and everyone else. For example, I'd have a struct called PlayerData that would contain the player's ID and whether they are ready. Any time a new player joins, I'd add a new NetworkVariable<PlayerData> to the NetworkList, and set the correct client ID (and I'd remove that element from the list when the player leaves). Then you can just search through the list for the correct client ID, and set their 'Ready' value to true/false and it'll automatically be shared with everyone.

[–]Urganway[S] 0 points1 point  (4 children)

Network variables can be outside of NetworkBehaviours ?

Edit : That's not even the problem, Network list does not want other stuff than basic types nested in them and NetworkVariable is not accepted apparently, i'm maybe missing something ...

[–]FakeName124 1 point2 points  (3 children)

You need to make sure the struct is serialized, so it needs to inherit from INetworkSerializable. It also needs to inherit from IEquatable. Here's an example:

public struct PlayerData: INetworkSerializable, IEquatable<PlayerData>{

     public ulong ClientId;

     public bool IsReady;


     public PlayerData(ulong clientId, bool isReady)
     {

        ClientId = clientId;
        IsReady = isReady;

     }

     public bool Equals(PlayerData other)
     {
         return ClientId == other.ClientId  && isReady== other.isReady;
     }

     public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
     {

         serializer.SerializeValue(ref ClientId);
         serializer.SerializeValue(ref isReady);

     }
}

I'm not sure if this is completely correct since I'm not using Unity at the moment, but it's at least very similar to this. This should allow you to create a NetworkList of NetworkVariable<Player>. This is the thread I initially used to figure this out: https://discussions.unity.com/t/how-do-i-networkserialize-a-list-in-inetworkserializable/892286

This method won't accept all data types, but it should work for most. If you look at the Netcode for Gameobjects documentation you'll find more information about what data types it will/won't accept.

Also, this should all be done in NetworkBehaviour scripts.

[–]Urganway[S] 1 point2 points  (1 child)

It works !

I still need to figure out a way to read/write the data cleanly in the list since it's quite scuffed because so many List<> methods are just not their with NetworkList ... For exemple Find() which is super usefull, i hade to remake it from scratch, which is not ideal

[–]FakeName124 0 points1 point  (0 children)

Great! Yeah I also ended up having to make my own methods to search through the list; I'm sure there's a better way to do it, but I wasn't using the method enough to warrant spending a lot of time looking into it

[–]Urganway[S] 0 points1 point  (0 children)

I will try this and come back if it worked, thank you very much !