Search Unity

Network Time

Discussion in 'Scripting' started by Nakel, Aug 14, 2018.

  1. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    hello there... I’m really trying to get my players to get the exact same event when time is up... and then it changes and time runs again.

    I did all the script all I need is a network time so I can prevent players get different events, and since Network.Time does no longer works I wonder what can I use now...
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Off the top of my head.....

    1) Server records time.time
    2) Server sends message to client requesting time.time
    3) Client responds with it's time.time
    4) Server receives response and subtracts its own time.time from step 1 from its current time.time to get approximate ping, and divides in half for approximate 1 way latency.
    5) Server adds 1 way latency to client's time.time, to get clients current time.time
    6) Server subtracts client's current time.time (from step 5) from server's current time.time to get client's offset from server's time.time
    7) Server decides on a time for an event and plans it according to its own time.time
    8) Server subtracts client's offset from server's current time.time (offset from step 6) and sends the result to the client to trigger an event at that time (the trigger time must be at least 1 way latency time in the future of the result sent)

    Or you could send the offset instead to the client, and the server can schedule events by sending the event time according to its own time.time, and the clients can easily calculate when to trigger the event against their own time.time using that offset.

    But in reality, ping times are generally 100ms or less, which mean 1 way latency is around half that. Players are not going to notice a 50ms difference in time of an event triggering, so I'd just have the server send a message at the time an event should trigger and ignore the slight differences in latency between clients.
     
    Last edited: Aug 15, 2018
    Doug_B likes this.
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I'd certainly second that. Just make a straightforward RPC from the server to all connected clients. Job done.

    Even if you were able to perfectly synchronise all connected parties at some given start point in time, there is still not a 100% guarantee that they would all fire at the exact same time in the future. For example, one machine could suddenly have a virus scan that start up and causes a slight delay. Or one of many other possible scenarios besides.

    It would only be worth trying to get that level of accuracy if it was really warranted.
     
    Joe-Censored likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    And it almost certainly is not.
     
    Doug_B likes this.
  5. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    What if the app checks if player is connected to internet.. then it shows news, events... limited time items stuff like that... they don’t join any room... that’s like main screen where information is shown and it changes time by time... but I want to show all players same news.. events items etc... that’s gonna change by time even if they are not connected. How do I sync that... I’m using PhotonNetwork.time but I don’t think works well..?
     
  6. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    ACTUALLY using Photon... PhotonNetwork.Time works!
    if you could give other answers would be cool too.

    And if you are not using photon but want to get a network time I found this way on the web.
    Code (CSharp):
    1.  void Start () {
    2.          StartCoroutine("getTime");
    3.      }
    4.      IEnumerator getTime()
    5.      {
    6.          WWW www = new WWW("http://www.saadkhawaja.com/gettime.php");
    7.          yield return www;
    8.          Debug.Log("Time on the server is now: " + www.text);
    9.      }
    though its not on seconds....
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    How are the clients getting "news" updates "even if they are not connected"? If you want something to work when not even connected I'd probably use DateTime.UtcNow.Ticks.

    The accuracy is really good (10 million or so ticks is a second or so I believe), but depends on their computer's hardware clock. Windows at least has had automatic time sync to an internet time server since at least Windows 7, so their hardware clock is usually reliable for anything but anti-cheat purposes at this point. I'm sure MacOS has been doing that for a while as well.
     
    Last edited: Aug 15, 2018
  8. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    I made a script that each 4 hours(timer) ítem changes for all players all it has to do is check online time less current timer, then all players will get the same look... I think I didn't express myself so well