question

thejamiryu avatar image
thejamiryu asked

Get server time

Greetings everyone.

What I am trying to do is save the player's last online time and relogin time, then I will calculate the offline time and progress.

handlers.GetServerTime = function(args, context) {
    return { "Time": new Date().getTime() };

I put this to my cloudscript but I have no idea how to call this from game's script.

Any guidance will be super helpful. Thank you.

CloudScript
10 |1200

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

thejamiryu avatar image
thejamiryu answered

I have slightly changed my code to these

handlers.getCurrentTime = function ()
{ 
var now = new Date();
return {
 
 
 result: now.getTime() / 1000
 
};
}
public void StartCloudGetCurrentTime()
    {
        ExecuteCloudScriptRequest request = new ExecuteCloudScriptRequest()
        {
            FunctionName = "getCurrentTime",
            GeneratePlayStreamEvent = true,
        };
        PlayFabClientAPI.ExecuteCloudScript(request, OnCloudGetCurrentTime, OnErrorShared);
    }
    private void OnCloudGetCurrentTime(ExecuteCloudScriptResult result)
    {
        Debug.Log(PlayFabSimpleJson.SerializeObject(result.FunctionResult));
    }
    private static void OnErrorShared(PlayFabError error)
    {
        Debug.Log(error.GenerateErrorReport());
    }

Now I'm getting this on Debug {"result":1589952711.009}

Can I change it to {1589952711.009} so I will convert it to int or something to store.

Thank you for your time @SethDu

10 |1200

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

Seth Du avatar image
Seth Du answered

In terms of the client side, you may call API ExecuteCloudScript or ExecuteEntityCloudScript to execute the function. In addition, because there are no strict limits for login APIs, you may write code to reduce the execution times on the client side. For example, only the first time of login for the day will trigger ExecuteCloudScript API and this verification process can be done locally on the client side.

In addition, if you are using Unity SDK, you may notice the focus change events, which is aimed for calculating the time. Please refer to: https://docs.microsoft.com/en-us/gaming/playfab/features/analytics/metrics/sessions.

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.

thejamiryu avatar image thejamiryu commented ·

Thank you for your answer @Sethdu. Yes I'm planning to trigger the code on first time of login. Then I can calculate the offline timer.

    handlers.GetServerTime = function(args, context) {
    return { "Time": new Date().getTime() };
public void StartCloudGetTime()
    {
        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
        {
            FunctionName = "GetServerTime",
            FunctionParameter = new { Time = serverTime },
            GeneratePlayStreamEvent = true,
        }, OnCloudGetTime, OnErrorShared);
    }
private void OnCloudGetTime(ExecuteCloudScriptResult result)
    {
        JsonObject jsonResult = (JsonObject)result.FunctionResult;
        object messageValue;
        jsonResult.TryGetValue("messageValue", out messageValue);
        Debug.Log((string)messageValue);
    }
private static void OnErrorShared(PlayFabError error)
    {
        Debug.Log(error.GenerateErrorReport());
    }

Now I'm trouble about "FunctionParameter". It's give me a RuntimeError. How can I correct this one.

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ thejamiryu commented ·

May I ask on which line does this error occur?

0 Likes 0 ·
thejamiryu avatar image thejamiryu Seth Du ♦ commented ·

I have slightly changed my code to these

handlers.getCurrentTime = function ()
{ 
var now = new Date();
return {
 
 
 result: now.getTime() / 1000
 
};
}

public void StartCloudGetCurrentTime()
    {
        ExecuteCloudScriptRequest request = new ExecuteCloudScriptRequest()
        {
            FunctionName = "getCurrentTime",
            GeneratePlayStreamEvent = true,
        };
        PlayFabClientAPI.ExecuteCloudScript(request, OnCloudGetCurrentTime, OnErrorShared);
    }
private void OnCloudGetCurrentTime(ExecuteCloudScriptResult result)
    {
        //Debug.Log(PlayFabSimpleJson.SerializeObject(result));
        Debug.Log(PlayFabSimpleJson.SerializeObject(result.FunctionResult));
    }

Now I'm getting this on Debug {"result":1589952711.009}

Can I change it to {1589952711.009} so I will convert it to int or something to store.

Thank you for your time @SethDu

1 Like 1 ·
Show more comments

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.