question

Game Developer avatar image
Game Developer asked

How can i set a virtual currency equal to a player statistics in a cloudscrip? can you give me the code

Hi i cant really understand things at the documentation, can you please give me a working code for this i would really appreciate, since i really dont have an idea of how this things in documentation really go on. i want to set a player statistic equal to a virtual currency

10 |1200

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

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

May I know why you want to set a virtual currency equal to a player statistics, and vice versa?

Now, to answer your question: How can i set a virtual currency equal to a player statistics in a cloudscript?

  1. Use GetPlayerStatistics to retrieve the value of a specific statistic.
  2. Use GetUserInventory to get the player’s current VC amount.
  3. Compare the VC value and the Statistic value, if the VC value is greater than the Statistic value, use SubtractUserVirtualCurrency to subtract the difference, otherwise, use AddUserVirtualCurrency to add the corresponding value.

The corresponding code is the following:

handlers.SetVCToStatistic = function (args, context){


    //Take Statistic 's' and the VirtualCurrency 'GC' for example:

    //1.Use GetPlayerStatistics to retrieve the value of a specific statistic.

    var statisticValue = server.GetPlayerStatistics({PlayFabId : currentPlayerId, StatisticNames: "s"}).Statistics[0].Value;  

    //2.Use  GetUserInventory to get the player’s current VC amount.


    var inventoryResult = server.GetUserInventory({PlayFabId: currentPlayerId});


    var vcValue = inventoryResult.VirtualCurrency.GC;

    // 3.Compare the VC value and the Statistic value, if the VC value is greater than the Statistic value, use SubtractUserVirtualCurrency to subtract the difference, otherwise, use AddUserVirtualCurrency to add the corresponding value.

    var diff = statisticValue - vcValue;

    if (diff > 0)
       server.AddUserVirtualCurrency({Amount: diff, PlayFabId: currentPlayerId, VirtualCurrency: "GC"});

    else if (diff < 0)
       server.SubtractUserVirtualCurrency({Amount: Math.abs(diff), PlayFabId: currentPlayerId, VirtualCurrency: "GC"})

    else 
        return;
}

You can use ExecuteCloudScript to execute it: https://docs.microsoft.com/en-us/gaming/playfab/features/automation/cloudscript/writing-custom-cloudscript#executing-cloudscript-functions-from-a-unity-game-client

10 |1200

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

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.