Forum rules - please read before posting.

"Limit Items From Container" Problems

I have a limited inventory system (like classic Resident Evil) and so am using the Limit Items From Container script found on the wiki page to prevent the player adding too many items to their inventory from the container, however it is not working for me.

It will not allow me to take any items at all from the container, even though my inventory holds less than the "Max Items", unless I empty my inventory completely, then it will allow them to be added again - but when added, they do not go to the selected slot, they only occupy slots in order, from left to right.
Another issue is it will only allow me to add 4 items even though I have specified the Max Items to be 8.

Also, if I select an item in the container, then close that container (without adding item to inventory), the item is selected as though it were an inventory item and can be used in the scene.

This is using AC 1.75.5 (I am planning on updating, but it takes me an entire day to make a backup these days)
Unity 2018.4.3

Comments

  • edited April 2023

    I'd need more details to recreate the issues. Can you provides screenshots of all the Inspectors/Editors involved - including your items, Menu elements and Settings Manager?

  • Okay, I have screens of container and inventory menu settings and the settings manager:

    Images

    Also here's a video showing the issues in action.

    If there is something else I should show let me know and I'll get on it, thanks.

  • This might have to be a case of dealing with things one-at-a-time.

    For the first transferring issue: the LimitItemsFromContainer script includes multiple items in the same slot when comparing it with the max items value. I've updated the script on the wiki with a checkbox to prevent this.

    For the second, wrong slot issue: I can't recreate this behaviour. If you disable the script, and set the PlayerInventory element's type back to Default, do items then appear in the correct slot?

  • It seems your update of the script has fixed both of those issues, even the wrong slot issue. I'll do more testing, but it seems good right now, thank you.

    There is one minor thing though: For swapping items it seems I can swap an inventory item with a container item, but not vice versa. Is there some reason behind this?

  • Sorry, which way around do you mean? Clicking the inventory first and then the Container?

  • edited April 2023

    I can click an item in the inventory and swap it with one from the container.
    I can not click an item in the container and swap it with one from the inventory.

    So clicking container first is the way that doesn't work.

  • Open up MenuInventoryBox and you should find two instances of the line:

    KickStarter.runtimeInventory.PlayerInvCollection.Insert (KickStarter.runtimeInventory.SelectedInstance, trueIndex);
    

    If you replace both with the following, does it work?

    KickStarter.runtimeInventory.PlayerInvCollection.Insert (KickStarter.runtimeInventory.SelectedInstance, trueIndex, OccupiedSlotBehaviour.SwapItems);
    
  • No different I'm afraid, swapping still only allowed one way.

  • edited April 2023

    I've updated the wiki script - revert the change above and use the new LimitItemsFromContainer script.

  • Yeah that's done it, thank you.

  • Oh wait, sorry it hasn't fixed it after all.

    If you select an item from the container and try and swap it with an inventory item, it does add the item to your inventory but it doesn't actually swap it with the inventory item, even bypassing the max amount set in the script/component.

  • Sorry, I'll need steps to recreate. What was different in testing when this issue occurs vs when it didn't?

  • edited April 2023

    Ah, I see what's happening now.
    It will swap items but only if the inventory is full.
    The issue is:
    If you try and swap an item from the container with an inventory item but the inventory is not full, it will just add the item to inventory instead, often taking up a slot in the next "column" that requires you to use the shift right button to be able to see it (if your inventory is set up that way), therefore undesirable.

    If you choose to select an item with a container item already selected, it should swap it with that item, regardless of whether there is free space in the player inventory *unless it is an identical, stackable item, in which case it would make sense to just add it to that amount.

  • It's a case of rewriting the custom script to suit your game's specific needs. To get that behaviour, try replacing the code block of lines 22-37 with:

    int numItems = KickStarter.runtimeInventory.PlayerInvCollection.GetCount (includeMultipleItemsInSameSlot);
    if (InvInstance.IsValid (inventoryBox.GetInstance (slot)))
    {
        Container container = selectedInstance.GetSourceContainer ();
        if (container)
        {
            KickStarter.runtimeInventory.PlayerInvCollection.Insert (selectedInstance, slot + element.GetOffset (), OccupiedSlotBehaviour.SwapItems);
            KickStarter.runtimeInventory.SetNull ();
        }
    }
    else if (numItems < maxItems)
    {
        inventoryBox.inventoryBoxType = AC_InventoryBoxType.Default;
        inventoryBox.HandleDefaultClick (MouseState.SingleClick, slot);
        inventoryBox.inventoryBoxType = AC_InventoryBoxType.CustomScript;
    }
    
  • Okay, it seems this kind of flips the issue around. With this code you can swap container items with inventory items, but you cannot swap inventory items with container items.

    There is a much worse effect though: For some reason you cannot put inventory items in the container at all. All I did was add an extra } to the end of the script as I was getting an error, surely that's nothing though.

  • It's highly likely these two things are related though, so if we can solve the latter we'll probably hit two birds with one stone.

  • I'm not sure I'm able to recreate the issue - and the script shouldn't affect the behviour of the Container either. However, give this tweaked script a try:

    using UnityEngine;
    using AC;
    
    public class LimitItemsFromContainer : MonoBehaviour
    {
    
        public int maxItems = 5;
        public bool includeMultipleItemsInSameSlot = true;
    
        private void OnEnable () { EventManager.OnMenuElementClick += OnMenuElementClick; }
        private void OnDisable () { EventManager.OnMenuElementClick -= OnMenuElementClick; }
    
        private void OnMenuElementClick (Menu menu, MenuElement element, int slot, int buttonPressed)
        {
            if (menu.title == "Container" && element.title == "PlayerInventory")
            {
                MenuInventoryBox inventoryBox = element as MenuInventoryBox;
                InvInstance selectedInstance = KickStarter.runtimeInventory.SelectedInstance;
    
                if (InvInstance.IsValid (selectedInstance))
                {
                    Container container = selectedInstance.GetSourceContainer ();
                    int numItems = KickStarter.runtimeInventory.PlayerInvCollection.GetCount (includeMultipleItemsInSameSlot);
    
                    if (InvInstance.IsValid (inventoryBox.GetInstance (slot)) && container)
                    {
                        KickStarter.runtimeInventory.PlayerInvCollection.Insert (selectedInstance, slot + element.GetOffset (), OccupiedSlotBehaviour.SwapItems);
                        KickStarter.runtimeInventory.SetNull ();
                    }
                    else if (numItems < maxItems)
                    {
                        inventoryBox.inventoryBoxType = AC_InventoryBoxType.Default;
                        inventoryBox.HandleDefaultClick (MouseState.SingleClick, slot);
                        inventoryBox.inventoryBoxType = AC_InventoryBoxType.CustomScript;
                    }
                }
                else
                {
                    InvInstance clickedInstance = inventoryBox.GetInstance (slot);
                    if (InvInstance.IsValid (clickedInstance))
                    {
                        clickedInstance.Select ();
                    }
                }
            }
        }
    
    }
    
  • No worries Chris, from what I've tried so far it seems your script here is working great. The inventory limits what I can take out of the container and I can swap items properly, thanks again.

    Also I just realised I need to get rid of the "Take All" button. I actually have it's Is visible? set to false but it still shows. I'm guessing there'll be no harm in just deleting it?

  • I actually have it's Is visible? set to false but it still shows.

    Odd - are you using Unity UI or AC for the Menu's rendering?

    If you have no need for it, though, it's OK to delete.

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.