Forum rules - please read before posting.

Is there a way to display the last inventory items added?

edited November 2023 in Technical Q&A

Hello, I'd like a summary windows (menu) which contains the last 4 inventory items added.
Now, consider I don't rely to the "Add to Front?" when collecting an item... is there a way to display only the last items collected via inventory box? Maybe attaching a script to my inventory box buttons? Or changing the Inventory Box type to Custom Script (and then basically using the default script, plus something custom...)?

Any guidance?

Comments

  • You can store a copy of the added items into a Container, which you then display in a set Menu Element using the Container: Open Action. This Container should be a prefab with both a Survive Scene Changes and a Remember Container component with Retain in prefab? checked, so that both it and its ID is persistent.

    To spawn it in at runtime, assign an ActionList on start game asset in the Settings Manager, and have it run an Object: Add or remove Action to add it to the scene.

    To add items to it as the Player also gets them, use the Events Editor to create an Inventory: Add event. Auto-create an ActionList, add a Container: Add or remove Action that updates this Container, and use its auto-generated "Inventory item" parameter to override which item gets added.

  • edited November 2023

    Thanks for your hints! Ok, so...
    I think I've done what you suggested (with some changes which I'll explain soon), but I can't make it work.

    So, first of all, my container is displayed in a menu which I've already created (it's called "Summary", and it contains an inventory box, type "container").

    The container is set to have max 4 slots, and I ticked "Swap when full", because I thought that this way only the last items added were displayed.

    I open the container at start, even if it's not displayed (and that's the correct behavior, I want to show it only when the menu Summary is called by the player via input).

    However, the items are not displayed in the order from the last added, and also, I cannot add more than 4 items, because later items are not swapping old ones for some reasons... (not sure it's a bug, I'm using AC v1.78.4)

    I'm not sure why you were suggesting to use Containers on the first place, I thought it was related with the swapping thing and/or with the order of how a container display items...?

    Because I could show my inventory items on different inventory box already, my problem was related with showing them in order from the last added ones (like in terms of "time of addition").

    So, again, what I need is an inventory box showing only the last added 4 items, better if ordered as the last added item is the first on the list and so on.

  • An InventoryBox element can't modify the contents of an Inventory Collection - only display it. I'm suggesting use of a Container so that it has only the items you wish to display already, no need to hide some / truncate the excess.

    the items are not displayed in the order from the last added,

    In what order are they displayed?

    I cannot add more than 4 items, because later items are not swapping old ones for some reasons...

    "Swapping" requires an item to be moved from one collection to another - in this case, we're simply adding copies to the Container.

    For more control over the placement of items (and removal of if too many are in it), you can use custom scripting:

    using UnityEngine;
    using AC;
    
    public class RecentItemsContainer : MonoBehaviour
    {
    
        public Container container;
    
        void OnEnable ()  { EventManager.OnInventoryAdd_Alt += OnInventoryAdd; }
        void OnDisable () { EventManager.OnInventoryAdd_Alt -= OnInventoryAdd; }
    
        void OnInventoryAdd (InvCollection invCollection, InvInstance invInstance, int amount)
        {
            if (invCollection == KickStarter.runtimeInventory.PlayerInvCollection)
            {
                container.Add (new InvInstance (invInstance));
    
                if (container.InvCollection.GetCount (false) > 4)
                {
                    container.InvCollection.InvItems.RemoveAt (0);
                }
            }
        }
    
    }
    
  • Thanks for your help.
    Now I understood why you suggested to use a Container.

    Regarding your question "In what order are they displayed?".
    They are displayed in order of addition (as said on the first message, I don't have the "add on top" option activated).

    Meaning, I pick item A, then I pick B, then I collect C, then a character give me item D, my container (as well as my inventory), shows them in order A, B, C, D.

    But what I'd like is to show for first the last added (only on that specific "last added items" menu/container, not on my standard inventory), therefore D, C, B, A. So when I add E, A is excluded and the container shows E, D, C, B.

    I've tried using the script you wrote, but it doesn't work.
    I removed everything you suggested earlier (in your first response), and now I'm using only that script.
    I also put Debug.Logs to track what it does, and every call is triggered properly (items are added, even if not in the order I'd like), but the container.InvCollection.InvItems.RemoveAt (0); doesn't remove anything from the container itself. (i.e. if I check the inspector at runtime I can see 7 or 8 items listed on that container, not just 4).

    I thought it was related to limiting the max number of slots, but if I do so, then simply the new items are not added (so only the first 4 are in the container).

  • Let's try simply adding items to the front - if the Menu only displays the first 4 slots, it doesn't matter how many are actually in the Container itself:

    void OnInventoryAdd (InvCollection invCollection, InvInstance invInstance, int amount)
    {
        if (invCollection == KickStarter.runtimeInventory.PlayerInvCollection)
        {
            container.InvCollection.Insert (new InvInstance (invInstance), 0);
        }
    }
    
  • Nice! Thanks! It seems to do the job this way!

  • One thing though... I realized that when I use an item or give it to someone (meaning: when this item disappears from my inventory), it's still displayed in the "Last Inventory Additions" container.
    The strange thing is that they don't rotate anymore (I've a a raw texture, with the item rotating). So it feels like it's a "ghost" of the actual item...?
    I tried implementing an "OnInventoryRemove" doing the opposite of the above, but it doesn't change much.
    Maybe it needs some sort of "refresh"?
    In the inventory they are correctly removed, but in the "Last Additions" container, they are still there, but "frozen" (like that the raw image is not perfectly wiped out).

    Also, there's another question... My inventory doesn't "re-arrange" when some items are removed, meaning: I see some "holes" in the inventory, which are filled only when I collect a new item. How can I re-arrange those? And how can I avoid similar issues with the container? According with the script above that you wrote, there's an "Insert" of an item in the InvCollection. But if some "holes" are created by removing items, how can I re-arrange everything?

    To summarize, the problems are two:

    1- I can't clean completely the Container from a raw texture when an item is removed from my inventory (and therefore should be removed from the container).

    2- The items are not re-arranged in the Inventory, leaving gaps (and I'm afraid this issue will be even more evident on the Last Inventory Additions container).

    Any hints?

    I'm attaching a picture showing the issue (imagine that each item rotate on itself along the Z axis, except the two cats of the bottom-right image)

    For the inventory, I'd expect that after removing the 3 cats, the other two items go to cells 1 and 2, rather than staying where they are.

  • I tried implementing an "OnInventoryRemove" doing the opposite of the above, but it doesn't change much.

    This'll be necessary if you want the UI to update when items are removed as well.

    What was your implementation? Try this:

    void OnInventoryRemove (InvCollection invCollection, InvInstance invInstance, int amount)
    {
        if (invCollection == KickStarter.runtimeInventory.PlayerInvCollection)
        {
            container.InvCollection.DeleteAllOfType (invInstance.ItemID);
        }
    }
    

    The items are not re-arranged in the Inventory, leaving gaps

    Uncheck Items can be re-arranged in Menus? in the Settings Manager.

  • Thanks again for your outstanding support!
    All is working nicely now!

    I was using:
    container.InvCollection.Delete(invInstance);

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.