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