Class NetworkTimeSystem
NetworkTimeSystem is a standalone system which can be used to run a network time simulation. The network time system maintains both a local and a server time. The local time is based on the server time as last received from the server plus an offset based on the current RTT - in other words, it is a best-guess effort at predicting what the server tick will be when a given network action is processed on the server.
Inherited Members
Namespace: Unity.Netcode
Assembly: Unity.Netcode.Runtime.dll
Syntax
public class NetworkTimeSystem
Constructors
NetworkTimeSystem(double, double, double, double)
The constructor class for NetworkTickSystem
Declaration
public NetworkTimeSystem(double localBufferSec, double serverBufferSec = 0.05000000074505806, double hardResetThresholdSec = 0.2, double adjustmentRatio = 0.01)
Parameters
Type | Name | Description |
---|---|---|
double | localBufferSec | The amount of time, in seconds, the server should buffer incoming client messages. |
double | serverBufferSec | The amount of the time in seconds the client should buffer incoming messages from the server. |
double | hardResetThresholdSec | The threshold, in seconds, used to force a hard catchup of network time. |
double | adjustmentRatio | The ratio at which the NetworkTimeSystem speeds up or slows down time. |
Properties
AdjustmentRatio
Gets or sets the ratio at which the NetworkTimeSystem speeds up or slows down time.
Declaration
public double AdjustmentRatio { get; set; }
Property Value
Type | Description |
---|---|
double |
HardResetThresholdSec
Gets or sets a threshold in seconds used to force a hard catchup of network time.
Declaration
public double HardResetThresholdSec { get; set; }
Property Value
Type | Description |
---|---|
double |
LocalBufferSec
Gets or sets the amount of time in seconds the server should buffer incoming client messages. This increases the difference between local and server time so that messages arrive earlier on the server.
Declaration
public double LocalBufferSec { get; set; }
Property Value
Type | Description |
---|---|
double |
LocalTime
The current local time with the local time offset applied
Declaration
public double LocalTime { get; }
Property Value
Type | Description |
---|---|
double |
ServerBufferSec
Gets or sets the amount of the time in seconds the client should buffer incoming messages from the server. This increases server time. A higher value increases latency but makes the game look more smooth in bad networking conditions. This value must be higher than the tick length client side.
Declaration
public double ServerBufferSec { get; set; }
Property Value
Type | Description |
---|---|
double |
ServerTime
The current server time with the server time offset applied
Declaration
public double ServerTime { get; }
Property Value
Type | Description |
---|---|
double |
Methods
Advance(double)
Advances the time system by a certain amount of time. Should be called once per frame with Time.unscaledDeltaTime or similar.
Declaration
public bool Advance(double deltaTimeSec)
Parameters
Type | Name | Description |
---|---|---|
double | deltaTimeSec | The amount of time to advance. The delta time which passed since Advance was last called. |
Returns
Type | Description |
---|---|
bool |
Reset(double, double)
Resets the time system to a time based on the given network parameters.
Declaration
public void Reset(double serverTimeSec, double rttSec)
Parameters
Type | Name | Description |
---|---|---|
double | serverTimeSec | The most recent server time value received in seconds. |
double | rttSec | The current RTT in seconds. Can be an averaged or a raw value. |
ServerTimeSystem()
Creates a new instance of the NetworkTimeSystem class for a server instance. The server will not apply any buffer values which ensures that local time equals server time.
Declaration
public static NetworkTimeSystem ServerTimeSystem()
Returns
Type | Description |
---|---|
NetworkTimeSystem | The instance. |
Sync(double, double)
Synchronizes the time system with up-to-date network statistics but does not change any time values or advance the time.
Declaration
public void Sync(double serverTimeSec, double rttSec)
Parameters
Type | Name | Description |
---|---|---|
double | serverTimeSec | The most recent server time value received in seconds. |
double | rttSec | The current RTT in seconds. Can be an averaged or a raw value. |