question

Opticgamer avatar image
Opticgamer asked

Can't figure out how to get the consume count of all items in player inventory

Hello I'm trying to make a script in unity that displays all the usage count of all items in the player's inventory. My code seems to only grab the last item in the players inventory and none of the others. I would like to have it grab each item and assign each of the items usage counts to a string.


Code:






using PlayFab;

using PlayFab.ClientModels;

using PlayFab.ProfilesModels;

using PlayFab.DataModels;

using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;



public class InventoryViewManager : MonoBehaviour

{

    public GameObject uiParent;

    public GameObject sampleCanvas;



    public Text Item1Value;

    public Text Item2Value;

    public Text Item3Value;

    public Text Item4Value;



    private Vector3 RowStart = new Vector3(0, 15);

    private Vector3 ItemOffset = new Vector3(0, 175);



    void Start()

    {

        sampleCanvas.SetActive(false);

    }



    public void ViewInit()

    {

        sampleCanvas.SetActive(true);

        PlayFabClientAPI.GetUserInventory(new PlayFab.ClientModels.GetUserInventoryRequest(), UpdatePlayerInventory, OnError);

    }



    public void UpdatePlayerInventory(GetUserInventoryResult result)

    {

        var pos = new Vector3(RowStart.x, RowStart.y);

        var parentOffset = uiParent.transform.position;



        foreach(var item in result.Inventory)

        {

            var itemView = uiParent.transform.Find(CatalogViewItem.getIconFromItemId(item.ItemId));



            Item1Value.text = item.RemainingUses.ToString();


        }

    }



    private void OnError(PlayFabError error)

    {

        Debug.Log(error);

        //PopupError.ShowErrorMessage("PlayFab", error.GenerateErrorReport());

    }

}



apisPlayer DataPlayer Inventory
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

·
Gosen Gao avatar image
Gosen Gao answered

In the course of traversing “result.Inventory”. Every item in inventory has been read. Code below will print ID and RemainingUses of each item.

foreach(var item in result.Inventory){
        Debug.Log(item.ItemId + " RemainingUses " + item.RemainingUses.ToString());
    }

According to your code, “Item1Value.text” should be the usage count of single item in player’s inventory. But “Item1Value.text” has been assigned more than once. It will display the last value.

If you want to display more than one item, each item should have its own text to show its usage count.

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.