question

Noah Branham avatar image
Noah Branham asked

Question about checking inventory

I have the following code, and I am wanting to know how I would set a bool if the user has an item, I have an item with the id AKM, I'm wanting to check if he owns it or not.

PlayerOwnsAKM is a bool I have already made previously in the script, I'm trying to set it to true or false based on if the user has the item with itemID AKM in his inventory.

GetUserInventoryRequest requestInventory = new GetUserInventoryRequest();


        PlayFabClientAPI.GetUserInventory(requestInventory, result =>
        {
            PlayerOwnsAKM = result.Inventory.Contains("AKM");
        }, error =>
        {
            Debug.Log(error.ErrorMessage);
        });
apis
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

·
v-humcin avatar image
v-humcin answered

Inventory contains a list of ItemInstance objects, each of which holds several pieces of information about the item, not just the name, so you will have to check the ItemId of each item in the list. This is an example of one way to accomplish this:

foreach(ItemInstance item in result.Inventory)
{
   if (item.ItemId == "Powerup0")
        powerupActive = true;
}

You can read more about the information contained in an ItemInstance here: https://docs.microsoft.com/en-us/rest/api/playfab/client/player-item-management/getuserinventory?view=playfab-rest#iteminstance

Hope this helps!

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.