question

Lingaiswaran Jayakumar avatar image
Lingaiswaran Jayakumar asked

How to Increment Player Data using PlayFab Cloudscript

I want to Get the players ECount data (Which stores the player's money. Example: 0.00005) from the server. Then increment it by 0.00001 (or 1/100000). I have loosely followed the example code from the docs here.

However when I run my script on Unity, there are no changes taking effect. I am new to Javascript and not sure if I am doing anything wrong here on the CloudScript side.

How can I Get the player data and increment it using CloudScript?

My current Javascript implementation in CloudScript:

function reward(args) {
  var playerData = server.GetUserInternalData({
        PlayFabId: currentPlayerId,
        Keys: ["ECount"]
  });
  
  var newECount = Number(playerData.Data["ECount"].Value) + (1/100000);




  var updateUserDataResult = server.UpdateUserInternalData({
    PlayFabId: currentPlayerId,
    Data:
        {
          "ECount": NewECount.toString()
        }
  });
}

My current C# implementation in Unity:

public void Reward()
    {

        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest
        {
            FunctionName = "reward"
        },
        result => {
            Debug.Log("CloudScript call successful");
            GetData(); //This external function retrieves the player data from PlayFab
        },
        error => {
            Debug.Log("CloudScript call failed");
            Debug.Log(error.GenerateErrorReport());
        });

    }

*I am getting the successful result callback in Unity editor, thus the function is definitely being called from the Client side.

Player DataCloudScript
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.

Lingaiswaran Jayakumar avatar image Lingaiswaran Jayakumar commented ·

Edit: There is a typo on line 16 of my CloudScript implementation. It should be newECount, not NewECount...However, even after the edit, the script is not running.

0 Likes 0 ·

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

As this documentation -- Writing custom CloudScript, especially this section said, you can only call CloudScript methods attached to the handlers JavaScript object. So, except fixing the typo you mentioned, you can add this function to your CloudScript to let it work.

handlers.reward = function (args, context) {
    reward(args);
}

Besides, have you learned about the PlayFab Virtual Currency? The Virtual Currency is the PlayFab built-in feature that designed for in-game economies. Players and characters can be granted these currencies, which can then be used to buy or trade items. This feature should be more suitable to handle the players’ money. Please check our documentation – Economy quickstart for more information. If you use this feature, you can increase the currency balance using Server API AddUserVirtualCurrency.

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.

Lingaiswaran Jayakumar avatar image Lingaiswaran Jayakumar commented ·

In the end I couldn't get the CloudScript to work even after making the edits to client and server. I wasn't able to debug the exact reason why as the Client side was definitely running the function after some tests.

So instead, I tried going down the Virtual Currency (VC) path as you mentioned. I was able to successfully increment the VC using AddUserVirtualCurrency.

May I know if PlayFab VC only accepts int values? As I was unable to set Float values through the server & client. I wasn't able to find any documentation regarding this, but my result VC returned an int value.

For now I'm using a Client side workaround by multiplying my VC by 0.00001f.

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.