Forum rules - please read before posting.

Container that shows the inventory items properties

Hi, I've been looking for the right way to implement this. Basically, I have a list of inventory items with a property called "Time Collected" and I want to display it in the container UI.

I've setup my container to have the textBox and made a custom script to load it but I can't make it work so I am now heading to the forums to pray to the AC God.

Here's how my inventory layout looks like:
https://imgur.com/a/C3Yk29r

So I want my inventory items to display the time directly on their lower right.

Comments

  • Also, I believe this should be implemented built-in because I imagine this should be a pretty common use-case (unless there is a built-in way and I'm just dumb to see it). So maybe in the future? 🤞

  • A custom script is necessary - you'd need to attach it to each UI Button in your prefab.

    With the latest AC release, it's possible to extract the Inventory Item Instance from the Button it's associated with without need for additional variables.

    This is brute-forced in an Update loop, so you'd probably want to move to e.g. OnEnable, but this should do it:

    using UnityEngine;
    using UnityEngine.UI;
    using AC;
    
    public class ShowPropertyText : MonoBehaviour
    {
    
        void Update()
        {
            var canvas = GetComponentInParent<Canvas>();
            var menu = KickStarter.playerMenus.GetMenuWithCanvas(canvas);
            var inventoryBox = menu.GetElementWithGameObject(gameObject) as MenuInventoryBox;
            int slotIndex = inventoryBox.GetSlotIndex(gameObject);
            var invInstance = inventoryBox.GetInstance(slotIndex);
            if (InvInstance.IsValid(invInstance))
            {
                string label = invInstance.GetProperty("Time Collected").GetValue();
                GetComponentInChildren<Text>().text = label;
            }
        }
    
    }
    
  • Thank you for your lightning fast response. I might be doing something wrong here. The slot index is always -1. To confirm, this is supposed to be applied to the btnSlots inside the Container Grid right?
    Here is an image of the hierarchy: https://imgur.com/rEJiUiN

    The highlighted part is where this code is placed:

    using UnityEngine;
    using UnityEngine.UI;
    using AC;
    using TMPro;
    
    public class EvidenceSlotDisplay : MonoBehaviour
    {
        [SerializeField] public TextMeshProUGUI collectionTimeText;
    
        void Update()
        {
            var canvas = GetComponentInParent<Canvas>();
    
            if (!canvas)
            {
                Debug.LogError("NO canvas found for evidence slot display");
                return;
            }
    
            var menu = KickStarter.playerMenus.GetMenuWithCanvas(canvas);
            if (!menu)
            {
                Debug.LogError("NO menu found for evidence slot display");
                return;
            }
            var inventoryBox = menu.GetElementWithGameObject(gameObject) as MenuInventoryBox;
            if (!inventoryBox)
            {
                Debug.LogError("NO inventory box found for evidence slot display");
                return;
            }
            Debug.Log("inventoryBox obj " + inventoryBox.name, inventoryBox);
            int slotIndex = inventoryBox.GetSlotIndex(gameObject);
    
            Debug.Log("Slot index " + slotIndex);
            if (slotIndex < 0)
            {
                Debug.LogError("Invalid slot index");
                return;
            }
    
            var invInstance = inventoryBox.GetInstance(slotIndex);
    
            if (InvInstance.IsValid(invInstance))
            {
                string label = invInstance.GetProperty("Time Collected").GetValue();
                collectionTimeText.text = label;
            }
        }
    
        void Awake()
        {
            // Automatically find the TMP component named "txtTime" in children, if not already assigned
            if (collectionTimeText == null)
            {
                Transform timeTransform = transform.Find("Time").Find("txtTime"); 
                if (timeTransform != null)
                {
                    collectionTimeText = timeTransform.GetComponent<TextMeshProUGUI>();
                }
                else
                {
                    Debug.LogWarning("[EvidenceSlotDisplay] Could not find 'txtTime' child object.");
                }
            }
        }
    
    }
    
    
  • I managed to fix it by changing line 676 in the MenuInventoryBox code from:

    if (uiSlots[i].uiButton && uiSlots[i].uiButton == gameObject)
    

    to:

    if (uiSlots[i].uiButton && uiSlots[i].uiButton.gameObject == gameObject)
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.