Fixes for issues with PhotonNetwork.time

Tobias
Tobias admin
In the current releases, we did serveral updates to PhotonNetwork.time, due to issues with IL2CPP export to iOS64 bit.
Here is some background and a fix, which you can apply right away to your PUN code. We will update PUN in the Asset Store early next week, too.

Background
We sync the time internally as integer (networkingPeer.ServerTimeInMilliSeconds). It's the server's Environment.TickCount value, which is different per machine. This makes it very different per room, too.
Environment.TickCount can be negative, but that's OK, as time-differences between any two timestamps should be correct (unless the times were 48+ days from each other).
For gaming purposes, it's just important that everyone in a room has the same synced time value.
PUN uses the ServerTimeInMilliseconds as basis for PhotonNetwork.time.

The cause for the recent PhotonNetwork.time changes, are problems in some Unity versions with IL2CPP. There were overflow issues and even in 4.6.5p4, an explicit cast to uint gets stripped out of the build.


Problem and Fix
Some game code is written with the assumption that PhotonNetwork.time is always positive, as it was until recently.

To avoid the negative values, it is enough to cast the ServerTimeInMilliseconds to an uint temporary value before using it.
Use this code to avoid negative time values:

[code2=csharp]// PhotonNetwork.cs
/// <summary>
/// Photon network time, synched with the server.
/// </summary>
/// <remarks>
/// v1.55</br>
/// This time value depends on the server's Environment.TickCount. It is different per server
/// but inside a Room, all clients should have the same value (Rooms are on one server only).</br>
/// This is not a DateTime!</br>
///
/// Use this value with care: </br>
/// It can start with any positive value.</br>
/// It will "wrap around" from 4294967.295 to 0!
/// </remarks>
public static double time
{
get
{
if (offlineMode)
{
return Time.time;
}
else
{
uint u = (uint)networkingPeer.ServerTimeInMilliSeconds;
double t = u;
return t / 1000;
}
}
}

//in PhotonMessageInfo (in PhotonClasses.cs)
public double timestamp
{
get
{
uint u = (uint)this.timeInt;
double t = u;
return t / 1000;
}
}[/code2]


As said, we will update PUN asap in the Asset Store.


Next Steps
We keep thinking about this topic but most likely it was simply a bad design decision to give you PhotonNetwork.time as double.
The time we get from the server is not a double and will overflow at some point, depending on how long the server is running already. That can be in three weeks or right after a few seconds. And as double, the overflow is harder to detect (goes from high value to 0) and it's harder to calculate time-deltas for events.

We will need more time and discussion to come up with a good solution.
Right now, please use the fix above.

Comments

  • I released v1.55 in the Asset Store. It should fix this issue and is only a minor update.