Synchronizing game timer between clients

Options
I'm working on a fps game and each room has a timer that when it hits 0, the room closes. I'm having trouble synchronizing the timer between clients, so when a new player joins the room, the timer for the new client starts at the time that I set in the code. How am I able to Synchronize the time between all clients.



Here is my code that changes the time, and the all relevant code is in the screen shot

Comments

  • Hi @ItsKhanny,

    the best approach for a synchronized timer is to store a start time (or in this case an end time) in the Custom Room Properties. After having successfully joined the room the OnJoinedRoom callback gets called on this called. If this client is the MasterClient, he can store a time value in the Custom Room Properties.

    As an example for storing the current time, the client can add PhotonNetwork.time to the Room Properties, which are automatically synchronized across all clients in the same room. Since PhotonNetwork.time is also the same value on all clients in the room, you can calculate the difference between this value and the one that is stored in the Custom Room Properties. The result of this calculation basically describes how long the game is running.
  • jonavuka
    Options
    Be careful of using PhotonNetwork.time as this value can loop over back to zero, from the documents it can loop from 4294967.295 to 0. This is because it's a double and increments over it's memory space
  • ItsKhanny
    Options
    Its been a few months now. And I recently picked up Photon again. I solved the problem by using this code, which actually works perfectly
    if (PhotonNetwork.IsMasterClient)
            {
                timer = timeLimit;
                Hashtable ht = new Hashtable() { { "Time", timer }};
                room.SetCustomProperties(ht);
            }
            else
            {
                timer = (float)room.CustomProperties["Time"];
            }
    void UpdateTimer()
        {
            timer -= Time.deltaTime;
            Hashtable ht = room.CustomProperties;
            ht.Remove("Time");
            ht.Add("Time", timer);
            room.SetCustomProperties(ht);
            if(timer <= 0)
            {
                EndMatch();
            }
        }