#Issue with Network List

1 messages · Page 1 of 1 (latest)

viral gull
#

Post your code here and we can take a look at it

short leaf
# viral gull Post your code here and we can take a look at it
    public struct PlayerInventoryItem : INetworkSerializable
    {
        public FixedString64Bytes ItemId;
        public int InventoryQuantity;
        public int OwnedQuantity;

        public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
        {
            serializer.SerializeValue(ref ItemId);
            serializer.SerializeValue(ref InventoryQuantity);
            serializer.SerializeValue(ref OwnedQuantity);
        }
    }
...
        public NetworkList<ForceNetworkSerializeByMemcpy<PlayerInventoryItem>> LocalPlayerInventory { get; private set; }

        private void Awake()
        {
            LocalPlayerInventory = new();
        }
...
LocalPlayerInventory.Add(playerInventoryItem);
...
#

help is much appreciated 😉

viral gull
#

Code there looks fine. Any errors popping up?

short leaf
#

no errors

#

I see that other NetworkVariables are also having trouble syncing to the client

#

Well, just the ones that were assigned on the host are accurately showing values

viral gull
#

Yea. by default only the host can Add to the Network List. But that should give an error if a client tries. if it doesn't that might be a bug

short leaf
#

I only modify the values with server rpc's

viral gull
#

weird, that shouldn't be an issue at all

short leaf
#

I'm speechless too

#

I'm writing a minimalist NetworkVariable to test

#

Okay I've got some results

#
        public NetworkList<ForceNetworkSerializeByMemcpy<PlayerInventoryItem>> TestVariable { get; private set; }

        [ServerRpc]
        private void TestServerRpc()
        {
            PlayerInventoryItem testitem = new("T_" + UnityEngine.Random.Range(0, 1000), 1, 1);
            TestVariable.Add(testitem);
            TestClientRpc();
        }

        [ClientRpc]
        private void TestClientRpc()
        {
            Debug.Log(TestVariable[0].Value.ItemId);
        }

        public override void OnNetworkSpawn()
        {
            if (!IsOwner) return;
            TestServerRpc();
        }
#

This results in the client throwing a nullreference at the debug part

viral gull
#

You'll need to initialize it in Start() or Awake(). Otherwise the clients remain uninitialized.

short leaf
#

forgot to include the awake part, but TestVariable = new(); is in the code at awake

viral gull
#

ok. Are the clients getting OnListChanged callback?

short leaf
#

yes, but interestingly this just happened:

#

(last message was incorrect so i deleted it)

#

it seems as if the client can't debug value zero of the list, but it can see the item in onlistchanged

short leaf
viral gull
short leaf
#

I guess I could subscribe to onlistchanged and have a function determine whether something was added or removed from the list by checking value and previousvalue

short leaf
viral gull
#

Network Variables get synced on spawn. But its normal to also subscribe to their OnValueChanged

short leaf
#

I'm also seeing that both the client and the host are calling the OnListChanged when one of the lists changes. This is expected behaviour, but how can I make sure that the method that the event invokes is only called on the client that 'owns' the list?

viral gull
#

I think changing the ReadPermissions to Owner will do that

short leaf
#

won't that allow the client to modify the list aka 'hack' new items into the list?

viral gull
#

no, the write permissions can still be Server