question

terrypapamarkou avatar image
terrypapamarkou asked

updating virtual currency after purchasing an item

we have just converted our inventory system over to the playfab economy. When the player logs in I am downloading their inventory and storing it locally to show their virtual currency amounts in the UI. When they purchase an item in the shop, I am waiting for that to successfully finish and then calling a cloud script function that returns the updated inventory and then I update the currency amount in the UI. Is there a cleaner way to do this?

My code.

public void HandleCatalogItemPurchase(string itemID, int cost)
{
    PlayFabClientAPI.PurchaseItem(new PurchaseItemRequest {
       
        CatalogVersion = GameStateManager.CatalogVersion,
        ItemId = itemID,
        Price = cost,
        VirtualCurrency = "GO"
    }, LogSuccess, LogFailure);
}

private void LogSuccess(PurchaseItemResult obj)
{
    SaveManager.Instance.RefreshTheInventory(RefreshTheCatalogButtons);
}
public void RefreshTheInventory(UnityAction callback)
{
    var request = new ExecuteCloudScriptRequest
    {
        FunctionName = "RefreshInventory",
        RevisionSelection = PlayFabManager.RevisionOption,
        GeneratePlayStreamEvent = PlayFabManager.LogCloudScript,
        FunctionParameter = new Dictionary<string, object>
        {
        }
    };

    PlayFabClientAPI.ExecuteCloudScript(request, result =>
    {
        
        if (result.Error == null)
        {
            var res = JsonReader.Deserialize<PlayFabManager.CloudResponse>(result.FunctionResult.ToString());
            if (res.Result == "success")
            {
                StorageDataUpdated(res.Data);
                if (callback != null)
                {
                    callback();
                }
                return;
            }
            else
            {
                Debug.Log("RefreshInventory result " + res);
             
                return;
            }
        }

    }, error =>
    {
        Debug.Log("RefreshInventory error " + error.ErrorMessage);
     
    });
}

Also, in regards to items with a time expiry, I read somewhere that the item expiry will only be checked when the inventory is 'touched'. This is probably ok for my game because we are often giving virtual currency rewards etc, but what about games that don't have that? Do they just query the inventory every minute or so to force the item to expire when required? And finally, how do I get the expiry time of the item so I can show it in the UI?

Thanks very much

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

·
Rick Chen avatar image
Rick Chen answered

>> Is there a cleaner way to do this?

Could you describe why you are using the CloudScript to update the inventory? You could just use the GetUserInventory API to get the updated inventory after the successful call of PurchaseItem API.

>> how do I get the expiry time of the item so I can show it in the UI?

The expiry time of a consumable-by-time item can be obtained from the result of the PurchaseItem API or GetUserInventory API.

>> what about games that don't have that? Do they just query the inventory every minute or so to force the item to expire when required?

I will provide an answer to this later. Your patience is appreciated.

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.

Rick Chen avatar image Rick Chen ♦ commented ·

>> what about games that don't have that? Do they just query the inventory every minute or so to force the item to expire when required?

You don’t have to query the inventory every minute, you can cache the expiry time in the client-side. The client can update the inventory when it is needed. For example, when the players want to refresh their inventory or use that item or when the client-side indicate that the item is expiring. Please note that once the item expired on PlayFab side, it will no longer exist in the player's inventory, any appending actions (via PlayFab API) on this item will not take effect.

0 Likes 0 ·
terrypapamarkou avatar image terrypapamarkou commented ·

Thank you for the response. I'm just calling the cloudscript because that's how I'm used to doing this sort of thing. What are the advantages of calling the API diurectly? Cleaner code? Faster response times? Thanks again

0 Likes 0 ·
Rick Chen avatar image Rick Chen ♦ terrypapamarkou commented ·

It is OK to call the API through CloudScript. The benefit of CloudScript is that it allows you to customize the function call, the disadvantage is that there are some limits in CloudScript, for example, the limit of Cloud Script execution time (API call) is 10 seconds. You could check the limit in [Your Game Manager]->[Title settings]->[Limits].

1 Like 1 ·
terrypapamarkou avatar image terrypapamarkou Rick Chen ♦ commented ·

ok thanks again

0 Likes 0 ·
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.