Table of Contents

Class NetworkTime

Namespace
Coherence.Core
Assembly
Coherence.Core.dll

NetworkTime encapsulates state and functionality for client-server synchronization. Step(double) should be called during Update and FixedUpdate to increment the ClientSimulationFrame. NetworkTime can operate in two different modes: normal mode and multi-client mode. In normal mode, the NetworkTimeScale should be applied to Time.timeScale each frame. This will affect the rate at which Unity triggers FixedUpdate on all game objects, adjusting the speed of the simulation to smoothly catch up with the server clock. This works well for most applications, however, since Time.timeScale is a global property, it cannot be used to synchronize multiple clients within the same application. For these scenarios, you can use the MultiClientMode which makes NetworkTime operate differently. With MultiClientMode enabled, Step(double) will apply NetworkTimeScale internally when incrementing simulation frames and when invoking the OnFixedNetworkUpdate. In order to adapt the simulation speed in multi-client mode, all MonoBehaviour simulation code should be moved from FixedUpdate into OnFixedNetworkUpdate event handlers. When working with multiple networked scenes with multi-client mode, it is also important to trigger PhysicsScene.Simulate for each scene from their respective OnFixedNetworkUpdate.

public class NetworkTime : INetworkTime
Inheritance
NetworkTime
Implements

Constructors

NetworkTime(Logger)

public NetworkTime(Logger logger = null)

Parameters

logger Logger

Fields

floatingPointTolerance

public const double floatingPointTolerance = 1E-06

Field Value

double

maxFrameDiffForHoldingTimeScale

public const int maxFrameDiffForHoldingTimeScale = 3

Field Value

int

maxTimeScale

public const double maxTimeScale = 1.5

Field Value

double

minTimeScale

public const double minTimeScale = 0.5

Field Value

double

simulationFrameResetTreshold

public const long simulationFrameResetTreshold = 256

Field Value

long

timeStep

public const double timeStep = 0.016666666666666666

Field Value

double

timeStepMs

public const double timeStepMs = 16.666666666666668

Field Value

double

Properties

AccountForPing

If true, the server frame will be adjusted by ping, resulting in the client frame matching the server frame as it is on the Replication Server the moment packet is received. If false, the client frame will aim to match the server frame as it was at the moment of being sent from the Replication Server.

public bool AccountForPing { get; set; }

Property Value

bool

ClientFixedSimulationFrame

Similar to ClientSimulationFrame but quantized to FixedTimeStep.

public AbsoluteSimulationFrame ClientFixedSimulationFrame { get; }

Property Value

AbsoluteSimulationFrame

ClientSimulationFrame

ClientSimulationFrame is the current network time quantized to 60hz. It is used to timestamp outgoing packets.

public AbsoluteSimulationFrame ClientSimulationFrame { get; }

Property Value

AbsoluteSimulationFrame

ConnectionSimulationFrame

The first ServerSimulationFrame received from the replication server. Used as a baseline for calculating SessionTime.

public AbsoluteSimulationFrame ConnectionSimulationFrame { get; }

Property Value

AbsoluteSimulationFrame

FixedTimeStep

The rate at which which OnFixedNetworkUpdate is invoked. Should normally be set to Time.fixedTimeStep. If set to zero, OnFixedNetworkUpdate will not be invoked.

public double FixedTimeStep { get; set; }

Property Value

double

IsTimeSynced

IsTimeSynced will be set to true the first time SetServerSimulationFrame(AbsoluteSimulationFrame, Ping) is called (usually when connecting). IsTimeSynced will be reset back to false by Reset(AbsoluteSimulationFrame, bool).

public bool IsTimeSynced { get; }

Property Value

bool

MultiClientMode

Allows multiple CoherenceBridge instances within one application to maintain independent time scales. This is useful for testing multiple connections within the Unity editor without making standalone builds. Instead of adapting Time.timeScale to catch up with server clock, it applies the frequency of OnFixedNetworkUpdate.

public bool MultiClientMode { get; set; }

Property Value

bool

NetworkTimeScale

The recommended time scale that should be applied to Time.timeScale for the client clock to smoothly catch up/slow down to server clock. TargetTimeScale is calculated during SetServerSimulationFrame(AbsoluteSimulationFrame, Ping) based on the current client/server frame diff, and NetworkTimeScale will smoothly approach this value over time.

public float NetworkTimeScale { get; }

Property Value

float

NetworkTimeScaleAsDouble

Similar to NetworkTimeScale with double precision.

public double NetworkTimeScaleAsDouble { get; }

Property Value

double

Pause

Pauses updating the client simulation frame. When set to true any calls to Step(double) will have no effect on the simulation frame and fixed simulation frame.

public bool Pause { get; set; }

Property Value

bool

ServerSimulationFrame

The last ServerSimulationFrame received from the replication server. Used for calculating TargetTimeScale in order to synchronize client with server.

public AbsoluteSimulationFrame ServerSimulationFrame { get; }

Property Value

AbsoluteSimulationFrame

SessionTime

Monotonic clock that only increases when connected to a replication server and that resets back to zero on disconnect. Value will jump considerably during time reset as client network time adapts to server time, see Reset(AbsoluteSimulationFrame, bool).

public float SessionTime { get; }

Property Value

float

SessionTimeAsDouble

Similar to SessionTime but with double precision.

public double SessionTimeAsDouble { get; }

Property Value

double

SmoothTimeScaleChange

Enables smoothing time scale changes, i.e. if there is a big gap between client and server simulation frames, instead of sudden jumps in time scale it will smoothly transition NetworkTimeScale towards TargetTimeScale.

public bool SmoothTimeScaleChange { get; set; }

Property Value

bool

TargetTimeScale

The time scale that NetworkTimeScale is smoothly approaching over time. When SmoothTimeScaleChange is set to false, TargetTimeScale and NetworkTimeScale are always equal.

public double TargetTimeScale { get; }

Property Value

double

TimeAsDouble

Monotonic clock that increases with each call to Step(double) regardless if connected to a replication server or not. Value will jump considerably during connect, disconnect and time reset as client network time adapts to server time, see Reset(AbsoluteSimulationFrame, bool).

public double TimeAsDouble { get; }

Property Value

double

Methods

Reset(AbsoluteSimulationFrame, bool)

Resets client and server frames to the given value. Happens on connect, disconnect and when client/server frames are too far apart. Sets IsTimeSynced to false, causing SessionTime to reset back to zero. Triggers OnTimeReset.

The new frame value that will be applied to both ClientSimulationFrame and ServerSimulationFrame. Default is frame zero.
public void Reset(AbsoluteSimulationFrame newClientAndServerFrame = default, bool notify = true)

Parameters

newClientAndServerFrame AbsoluteSimulationFrame
notify bool

SetServerSimulationFrame(AbsoluteSimulationFrame, Ping)

Updates ServerSimulationFrame and recalculates TargetTimeScale. TargetTimeScale will be proportional to the distance from the client simulation frame to the server simulation frame. The first time this method is called, it will trigger Reset(AbsoluteSimulationFrame, bool), causing ClientSimulationFrame to reset to ServerSimulationFrame. Subsequent calls will only trigger Reset(AbsoluteSimulationFrame, bool) if the client frame has drifted away from the server frame by at least simulationFrameResetTreshold frames.

public void SetServerSimulationFrame(AbsoluteSimulationFrame frame, Ping ping)

Parameters

frame AbsoluteSimulationFrame
ping Ping

Step(double)

Updates the ClientSimulationFrame using the current game time, advancing one frame every 1/60 second. The NetworkTimeScale is smoothly interpolated towards TargetTimeScale with each call. With multiClientMode enabled, however, the callback trigger rate is scaled by NetworkTimeScale.

public void Step(double currentTime)

Parameters

currentTime double

The current game time, usually just Time.time. Must be non-zero for OnFixedNetworkUpdate to be invoked.

Events

OnFixedNetworkUpdate

With MultiClientMode enabled it is recommended to put simulation code in OnFixedNetworkUpdate event handlers instead of FixedUpdate. This allows multiple connected clients within the application to execute at different frequencies for correct network synchronization. The Time.fixedDeltaTime should be applied similar to how it is normally used in FixedUpdate.

public event Action OnFixedNetworkUpdate

Event Type

Action

OnLateFixedNetworkUpdate

Similar to OnFixedNetworkUpdate but guaranteed to be called later.

public event Action OnLateFixedNetworkUpdate

Event Type

Action

OnServerSimulationFrameReceived

Triggered in response to SetServerSimulationFrame(AbsoluteSimulationFrame, Ping) after TargetTimeScale has been recalculated. The first parameter is the server simulation frame. The second parameter is the client simulation frame.

public event Action<AbsoluteSimulationFrame, AbsoluteSimulationFrame> OnServerSimulationFrameReceived

Event Type

Action<AbsoluteSimulationFrame, AbsoluteSimulationFrame>

OnTimeReset

Triggered the first time SetServerSimulationFrame(AbsoluteSimulationFrame, Ping) is called and at any subsequent call if ClientSimulationFrame and ServerSimulationFrame have drifter too far apart.

public event Action OnTimeReset

Event Type

Action