question

maria-1 avatar image
maria-1 asked

Trying to implement a reusable class for purchasing items using Economy v2 PlayFab

I am trying to implement a generic class that i can throw on all my inventory items in the game and depending on the glimmersPrice and itemName it will determine which item is being purchased on playfab because the price amount and the itemName will match the PriceAmounts and Items that the PurchaseInventoryItems API requires. I am getting this error though:

Cannot implicitly convert type 'int' to 'System.Collections.Generic.List<PlayFab.EconomyModels.PurchasePriceAmount>' [Assembly-CSharp]csharp(CS0029)



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using PlayFab.EconomyModels;

public class ItemToBuy : MonoBehaviour
{
    public int glimmersPrice;
    public string itemName;


    public void BuyItem2() {
        var request = new PurchaseInventoryItemsRequest {
        PriceAmounts = glimmersPrice;

        };
        PlayFabEconomyAPI.PurchaseInventoryItems(request, OnPurchaseInventoryItemsSuccess, OnError);
    }

 

I had a class very similar to using economy v1 and it worked, but now that I am trying to use the new version of the economy I am running into issues. I have been refrencing the documontation alot but I have found it is not very in depth.
In-Game EconomyPlayer Inventorysupport
10 |1200

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

Gosen Gao avatar image
Gosen Gao answered

According to Inventory - Purchase Inventory Items - REST API (PlayFab Economy) | Microsoft Learn, the PriceAmounts is not a int value. This parameter is to specify the expected price to purchase current item. To call this API, you also need to specify the parameters Item and Amount

You can refer to the request below.

{
  "Item": {
    "Id": "11111111-1111-1111-1111-111111111111"
  },
  "Amount": 1,
  "PriceAmounts": [
    {
      "ItemId": "cccccccc-cccc-cccc-cccc-cccccccccccc",
      "Amount": 15
    }
  ],
  "DeleteEmptyStacks": false
}
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.

maria-1 avatar image maria-1 commented ·

Yeah I know but is there any way I can set the item equal to my ItemName variable I have? That way I can reuse the code instead of having to create a call for each item in my playfab inventory?

0 Likes 0 ·
kylemc@microsoft.com avatar image
kylemc@microsoft.com answered

If you create the CatalogItems with an Alternate Id of Type = "FriendlyId" and Value = "{ItemName}", then you can use that re-use that name to get the item in the GetItem call.

Catalog - Create Draft Item - REST API (PlayFab Economy) | Microsoft Learn

Catalog - Get Item - REST API (PlayFab Economy) | Microsoft Learn

2 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.

maria-1 avatar image maria-1 commented ·

Isnt GetItem call a api call for Economy v1? I am trying to use Economy v2. Would you be able to show me how your explenation would be written out based on my code posted above please?

0 Likes 0 ·
Gosen Gao avatar image Gosen Gao maria-1 commented ·

It is an Economy v2 API, all v2 APIs are list in Catalog - REST API (PlayFab Economy) | Microsoft Learn and Inventory - REST API (PlayFab Economy) | Microsoft Learn. To purchase item with Friendly ID you set, you can refer to:

{
  "Item": {
    "AlternateId":{
        "Type":"FriendlyId",
        "Value":"GSTS_item"
    }
  },
  "Amount": 1,
  "PriceAmounts": [
    {
      "ItemId": "11111111-1111-1111-1111-111111111111",
      "Amount": 10
    }
  ],
  "DeleteEmptyStacks": false
}
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.