I’ve been doing some testing with the NetworkList<T> object, and I’m having a hard time understanding if I’m using it as intended…
My question boils down to, does the following code send ten separate network requests? I see that the below code will fire ten OnListChanged events, so I’m assuming this is ten requests?
for(int i = 0; i < 10; i++) { myDummyNetworkList.Add(i); }
and if yes, is there a way to bulk set the list so that I only send one?
I’m using a NetworkList<T> to store a set of integers so that all clients can read them after they’re set on the server. I will never add to the list, but I will clear it and set ten new values.
Would is be more efficient in this case to store the set of integers in a NetworkVariable<T> as a comma separated string and rely on a single OnValueChanged event?
No, since you’re changing the list ten times in the same tick. The list’s state (or its delta) is synchronized the next time network “ticks”.
This can pose another issue however, that is if you have a net var that you increment from 1 to 10 in the same tick but you expect every value change event (from 1 to 10) to fire for remote clients - that isn’t going to happen. Remote clients get one event with number 10 (the most current value).
Thank you, super helpful!
I guess a followup, it seems like the main benefit of the NetworkList is the ability to add / remove items in the list. If I don’t ever intend to add / remove an item (just reinit with ten new items), don’t care about the ten OnListChanged events, and am using a “simple” data type like an int, does it make more sense to just use a comma separated string as a NetworkVariable?
My impression is that these two approaches are more or less similar, are there any benefits / drawbacks to either?
In that case you may not even need a NetworkList. If you do not have late joining clients that need to get the current values when they join, you can simply send an array of values via RPC to all clients. Although I’m not 100% sure if array parameters are natively supported or require an INetworkSerializable implementation.
Since you have a list of integers, consider how big those integers need to be. If those values are in the ranges 0-255 or 0-65535 then using a byte or ushort type would cut down traffic to 25% or 50% respectively.
And that’s also why you would want to avoid strings. The number 65535 as ushort is 2 bytes but as a string it’s 5 bytes. Then add the commas to that and you can see that sending a string will likely send several times the data than would be necessary.
This may seem like a detail but it’s not if you use any services where you’d pay for traffic, like game server hosting or cloud save. And ultimately it may make your game have a bit less latency overall considering that you can send at most (close to) 1500 bytes in a single, non-fragmented packet over the Internet. Larger packets may get fragmented, and this may increase latency. That kind of thing only matters if you make a fast paced game though.