How to add item history

How would you go about adding an item history of picked up items on the player HUD?.

A history would be pretty complex… as you’ll probably need a scrollview to add the history entries to. I’ll get you started, but to describe writing a full history will require me to write a lengthy tutorial, and there is a bit of a queue for tutorials (I try to work on one at a time, I’ll add this to the list).

The Hud itself isn’t really set up for the scrollview required for a history, but it would be fairly easy to show the last item picked up, assuming you’re already into the Inventory course (I’ve added the inventory tag to the post).

First, in Inventory.cs, add the following field and methods:

InventoryItem lastItemPickedUp=null;
public string GetLastItemString()
{
    if(lastItemPickedUp!=null) return $"Last Item: {lastItemPickedUp.GetDisplayName()}";
    return "";
}

and in AddToFirstEmptySlot() add this line

if(item!=null) lastItemPickedUp = item;

You’ll need a Text component added to the hud, and a quick and dirty script

using UnityEngine;
using UnityEngine.UI;
using GameDevTV.Inventories;

public class LastItemUI:Monobehavior
{
     [SerializeField] Text text;
     Inventory playerInventory;
     
      void Awake()
      {
           playerInventory = Inventory.GetPlayerInventory();
       }

       void OnEnable()
       {
            playerInventory.InventoryUpdated+=UpdateLastItem;
            UpdateLastItem();
       }
       void OnDisable()
       {
              playerInventory.InventoryUpdated-=UpdateLastItem;
        }
   
        void UpdateLastItem() 
        {
              text.text = playerInventory.GetLastItemAsString();
        }
}
2 Likes

Thanks adding the scroll view would be very easy
From here I can take over. Because already added a damage history . Thank you for the help

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms