Hello everyone,
I'm trying to return server time to a client when the client logs in to calculate some time based events. This is a one time process when the player logs in. When I return the time from the server, how can I read it correctly in C#?
I tried "double" variable but it sometimes works, sometimes doesn't. For example; an event that has a countdown of 1 minute is sometimes shown as 3 minutes.
Here's how I return the time from server.
handlers.GetServerTime = function(args, context) { return { "Time": new Date().getTime() }; }
Here's how I convert the value in the game.
private void OnGetServerTimeSuccess(ExecuteCloudScriptResult data) { JsonObject jsonResult = (JsonObject)data.FunctionResult; object value; if (jsonResult.TryGetValue("Time", out value)) { ServerTime = double.Parse(value.ToString()); } }
And here's how I calculate the countdown. I save a time in the players' read only data. When they login, I substract two values to get how much time left.
double miliseconds = loginResult.UserReadOnlyData.ContainsKey("FreeLootbox") ? : double.Parse(loginResult.UserReadOnlyData["FreeLootbox"].Value) : ServerTime; double difference = miliseconds - ServerTime; DateTime nextLootbox = difference > 0 ? DateTime.Now.AddMilliseconds(difference) : DateTime.now;