question

Alen Jose avatar image
Alen Jose asked

Getting the server time.

Hi so I've made a request in Unity C# : GetTimeRequest time = new GetTimeRequest();.

How do I get the time value stored in the time variable?

apis
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Alen Jose avatar image Alen Jose commented ·

I also tried:

GetTimeResult time = new GetTimeResult();

Debug.Log(time.Time);

But that just gave me 01-01-0001 00:00:00 everytime i called it.

0 Likes 0 ·

1 Answer

·
Jordan avatar image
Jordan answered

Hi Alen.

You can try this out, where a function calls a new GetTimeRequest, then calls another function (passing in the result) when it succeeds. A third function is written to catch any errors.

    void GetCurrentTime()
    {
        PlayFabServerAPI.GetTime(new GetTimeRequest(), OnGetTimeSuccess, LogFailure);
    }
    void OnGetTimeSuccess(GetTimeResult result)
    {
        Debug.Log("The time is: " + result.Time);
    }
    void LogFailure(PlayFabError error)
    {
        Debug.Log("There was a problem getting the time. Error: " + error.GenerateErrorReport());
    }

GetTimeResult time = new GetTimeResult(); will only make a new, blank instance of a GetTimeResult (which is why it gave you 01-01-0001 00:00:00);

Hope this helps! Let me know if you have any other questions.

5 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Alen Jose avatar image Alen Jose commented ·

Thanks a lot!

0 Likes 0 ·
Alen Jose avatar image Alen Jose commented ·

I'm on the free tier of playfab. So can I make this call every second? Assuming we get a good number of players will this lead to a throttle?

0 Likes 0 ·
Jordan avatar image Jordan ♦ Alen Jose commented ·

I typically would not recommend making any calls that often if it could be avoided. Is there a particular reason you need it to be every second?

If you are wanting to display the time, you could get the server time once and start a clock function on the client side that uses system time. You could also check in with the server every other minute (for example) to ensure that the client and server times are in sync.

0 Likes 0 ·
Kenny Roy avatar image Kenny Roy commented ·

In the cloudscript example, there is this:

    server.UpdatePlayerStatistics(request);
    server.UpdateUserInternalData({
        PlayFabId: currentPlayerId,
        Data: {
            last_move_timestamp: new Date(now).toUTCString(),
            last_move: JSON.stringify(playerMove)
        }
    });

if I am similarly using cloudscript to do a function with time, can I just use the Date(now).toUTCString() function as opposed to doing a server.GetTimeRequest()?

0 Likes 0 ·
JayZuo avatar image JayZuo ♦ Kenny Roy commented ·

It's OK to use "new Date().toUTCString()" in CloudScript instead of calling server.GetTime as CloudScript is already the "server environment".

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.