Should I use PhotonNetwork.time in Update function?

Options
Hello,

I would like to do a countdown timer based on Photon server time.

Right now, my codes:
 
void Update()
    {
        timeLeft = (float) PhotonNetwork.room.CustomProperties["Game_Time"];
        if (isCountingDown && timeLeft > 0)
        {
            timeLeft -= Time.deltaTime;
        }

        if (isCountingDown && timeLeft <= 0)
        {
            isCountingDown = false;
            // Times up
        }
    }
Questions:
1. As you can see, in the update function, the timeLeft variable value gotten from room props. Will it slow the performance? As in, every time i call the room properties Update function, will it cause a lot of network traffic?

2. Same thing for the Time.deltaTime, if i replace the value with PhotonNetwork.time inside the Update function, will it effect the performance or network traffic?

Please advise. Thanks.

Comments

  • Hi @zaki,

    Will it slow the performance?


    I don't think so, it's a very fast look-up.

    As in, every time i call the room properties Update function, will it cause a lot of network traffic?


    If you update the Room Properties you will cause some network traffic. According to the code snippet you provided, you just read from the Room Properties, which are stored locally, too. Unless they are not updated and you only read from them, you don't cause 'any' network traffic (except from other, necessary traffic, e.g. keeping the connection alive).

    Same thing for the Time.deltaTime, if i replace the value with PhotonNetwork.time inside the Update function, will it effect the performance or network traffic?


    No. PhotonNetwork.time gets synchronized with server once and is locally available afterwards. If this however runs out of sync, you can use PhotonNetwork.FetchServerTimestamp() to get an up to date value again.

    Some hints for the countdown: When the timer actually gets started by the MasterClient, he can store the start time (PhotonNetwork.ServerTimestamp) and a bool value telling the timer has started in the Custom Room Properties. Other clients will receive the value when void OnPhotonCustomRoomPropertiesChanged(Hashtable propertiesThatChanged) is called. This callback can be used to store the start time locally. In their local update function they can now calculate the difference between PhotonNetwork.ServerTimestamp and the stored start time and use that difference to actually calculate the timer.

    Hope that helps you implementing the timer in your application. If you have further questions, please feel free to ask.
  • zaki
    Options
    Hello @Christian_Simon ,

    Thanks for the quick reply and suggestions. I have implemented as you suggested, and it works well.

    Best regards,
    Zaki