top of page
Search

Dev Log#16: Learning About Time. Syncing Match Time across players

Continuing on with functionality on the player controller class. The time and more importantly the syncing of time is handled on the player controller class.

To begin with I learned about Round Trip Time, which is just the amount of time it takes to send data from client to the server and from then server back to the client. So a pretty crucial metric for network programming.


In Unreal Engine you can use RPCs to communicate between the server and client.

The first part of the process is that the client asks the server what the current time is. The next step is the server reporting back to the client with the current time on the server. Then the last step is considering the factors that would affect the time as viewed by the player and adjusting for that through calculation.


I'm sending the request to the server via a server RPC called "RequestServerTime" that has a float parameter. I'm passing in the unreal engine function GetWorld->GetTimeSeconds as this will retrieve the games actual time in seconds. In doing so the server can appropriately respond.


In the request server time function, the server makes a note of the time it received the request from the client by getting the game's actual time in seconds like the input parameter for the function and storing this inside a local float variable to save this information. This float variable is called "ServerTimeofRequestReceived" and is important for the next step part of the process as we need to use it as a input parameter.


I've also created a client RPC called "ReportServerTime" that has two float parameters. This function is being called in the request server time function and the input parameter from that function and the local variable "ServerTimeofRequestReceived" is being passed into the report server time function. Another way to think of it is the time of when the request was made and the time when the server received the request is being passed into the report server time function.


In the report server time function I'm now able to calculate and store the round trip time in its own float variable by getting the games time in seconds and subtracting the time of when the request was first made, which is one if the parameters of the function. I'm then working out the current server time by estimating each trip of the round trip time as 1/2 of the round trip. An alternate way to find the exact time of each time would be to record the time when the server sends its report and the time when the client receives that report.

For now I'm calculating the current server time as the time the server received the request plus 0.5 multiplied by the round trip time variable.


Lastly I created a refresh function so that the time gets updated after a few seconds to maintain the time sync between all machines. This function will have delta time passed into as it will be called in the tick function and delta time will track the time past since the last refresh. I've also created a time update frequency variable so that I can set the refresh to happen every few seconds rather than every tick. The refresh time function also has a local controller check so that each client in game calls the server rpc because a server rpc will only run on the server if the controller is owned by the invoking client.


 
 
 

Commentaires


bottom of page