NetworkTime
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.
Constructors
NetworkTime |
Fields
floatingPointTolerance | |
maxFrameDiffForHoldingTimeScale | |
maxTimeScale | |
minTimeScale | |
simulationFrameResetTreshold | |
timeStep | |
timeStepMs |
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. |
ClientFixedSimulationFrame | Similar to ClientSimulationFrame but quantized to FixedTimeStep. |
ClientSimulationFrame | ClientSimulationFrame is the current network time quantized to 60hz. It is used to timestamp outgoing packets. |
ConnectionSimulationFrame | The first ServerSimulationFrame received from the replication server. Used as a baseline for calculating SessionTime. |
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. |
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). |
MaximumDeltaTime | Limits time incremented in Step(double) in case of cpu spikes. Should normally be set to Unity's Time.maximumDeltaTime. |
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. |
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. |
NetworkTimeScaleAsDouble | Similar to NetworkTimeScale with double precision. |
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. |
ServerSimulationFrame | The last ServerSimulationFrame received from the replication server. Used for calculating TargetTimeScale in order to synchronize client with server. |
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). |
SessionTimeAsDouble | Similar to SessionTime but with double precision. |
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. |
TargetTimeScale | The time scale that NetworkTimeScale is smoothly approaching over time. When SmoothTimeScaleChange is set to false, TargetTimeScale and NetworkTimeScale are always equal. |
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). |
Methods
Reset | 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. |
SetServerSimulationFrame | 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. |
Step | 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. |
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. |
OnLateFixedNetworkUpdate | Similar to OnFixedNetworkUpdate but guaranteed to be called later. |
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. |
OnTimeReset | Triggered the first time SetServerSimulationFrame(AbsoluteSimulationFrame, Ping) is called and at any subsequent call if ClientSimulationFrame and ServerSimulationFrame have drifter too far apart. |