Syncing Timer

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Syncing Timer

forgemaster
2020-10-10 21:15:03

Hello I've been having an issue for a while now that pops up once in a while and was wondering if anyone has faced this issue or if there is a better way of approaching this with photon?

So the way our time synchronization works is once there is 2 players in the room the masterclient sends a rpc with the UnixTimestamp for when the game should start, with the RpcTarget set as AllBuffered/AllBufferedViaServer (both don't fix the problem). Then the client adds the unixtimestamp to the local DateTime variable therefore everyone should technically have the same timer regardless of whether someone joins in 5 seconds later.
However for some reason we are testing with 3 players and there is always at least 1 player who is 5 seconds behind and the other two are a few seconds apart.

Here is our code:

private DateTime endDate;

void Update() {  
    if ((float)endDate.Subtract(DateTime.UtcNow).TotalSeconds < 0f) {  
        // start game  
    } else {  
        // update timer text ui  
    }  
}

// NOTE: this function is in another class ***  
public override void OnplayerEnteredRoom(Player newPlayer) {  
    if (PhotonNetwork.IsMasterClient && players.Length == 2) // 'players' var is a cached PhotonNetwork.PlayerList that is updated whenever someone joins and leaves  
        player.GetComponent<Timer>().InitiateStartTime(); // 'player' is reference to the local player gameobject  
}

public void InitiateStartTime() { photonView.RPC("UpdateStartTime", RpcTarget.AllBufferedViaServer, (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds + 30f)); }

[PunRPC]  
private void UpdateStartTime(long unixTimeStamp) {  
    GameObject[] players = GameObject.FindGameObjectsWithTag("Player");  
    foreach (GameObject player in players) {  
        if (player.GetPhotonView().IsOwnerActive) {  
            player.GetComponent<Timer>().endDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(unixTimeStamp);  
        }  
    }  
}  

Comments

JPGOrdon
2020-10-13 09:14:11

Can't you simply using Photon's provided timestamps for RPCs? You could even save a time stamp from an RPC, particularly the one that starts the game. Check here.

Afterlife1707
2021-06-18 16:17:09

any update on this? the documentation is not so clear and I'm unable to solve this problem.

Back to top