SyncTypes
Like remote procedure calls, SyncTypes are another type of communication. These are fields which automatically synchronize over the network to clients when the server changes them. There are a variety of SyncTypes available: SyncVar, SyncDictionary, SyncList, custom SyncTypes, and more.
When changes are made to a SyncType, only the changes are sent. For example, if you have a SyncList of 10 values and add another, only the just added entry will be sent.
SyncVar Values will be reset before OnDestroy
runs on the object, so you won't be able to get them in that method.
SyncTypes will also automatically generate serializers for custom types.
Host Client Limitations
There is a small limitation with all SyncTypes when running both the client and server in a single build.
While as the host client, the previous value, when applicable, in callbacks will be the current value if the asServer
parameter is false. This is mostly noticed in SyncVars, note the example below:
// This example assumes you are acting as the host client.
// We will imagine previous value was 10, and next is 20.
private void _mySyncVar_OnChange(int prev, int next, bool asServer)
{
// If asServer if true then the prev value will correctly be 10,
// with the next being 20.
// If asServer is false then the prev value will be 20, as well the
// next being 20. This is because the value has already been changed
// on the server, and the previous value is no longer stored.
DoSomething(prev, next);
}
There is a fairly simple way to accommodate this scenario if you plan on using a host client in your game.
// This example assumes you are acting as the host client.
// We will imagine previous value was 10, and next is 20.
private void _mySyncVar_OnChange(int prev, int next, bool asServer)
{
// Only run the logic using the previous value if being called
// with asServer true, or if only the client is started.
if (asServer || base.IsClientOnlyStarted)
DoSomething(prev, next);
}
Last updated