#Issue with Network List
1 messages · Page 1 of 1 (latest)
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 😉
Code there looks fine. Any errors popping up?
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
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
I only modify the values with server rpc's
weird, that shouldn't be an issue at all
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
You'll need to initialize it in Start() or Awake(). Otherwise the clients remain uninitialized.
forgot to include the awake part, but TestVariable = new(); is in the code at awake
ok. Are the clients getting OnListChanged callback?
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
must I wait for the OnListChanged callback before doing anything with the updated list on the client's side?
It hasn't updated until you get that callback
okay... I'm wondering how I can add that to my GiveItemToPlayer methods (serverrpc -> clientrpc)
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
Thanks to the OnListChanged callback I got the networklist syncing correctly. Is this also required for regular NetworkVariables? That'd make it a pain to work with...
Network Variables get synced on spawn. But its normal to also subscribe to their OnValueChanged
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?
I think changing the ReadPermissions to Owner will do that
won't that allow the client to modify the list aka 'hack' new items into the list?
no, the write permissions can still be Server