Forum rules - please read before posting.

InvItem.count sometimes returns 1 even if I have multiple items in inventory

edited April 2023 in Technical Q&A

Hi there,
I have a script that sets a variable to true if the selected item is multiple in inventory in this way:

private void OnInventorySelect(InvItem invItem)
    {
        if (invItem.count > 1)
        {
            AC.GlobalVariables.SetBooleanValue(28, true);
        }
    }

this works flawlessly if I give myself the item by checking "carry on start": writing in console invItem.count returns the actual number of item in inventory and boolean is set to true, but somehow when I take the item through the action Inventory-> Add invItem.count is always 1. It happens both if selection mode is single or all.
Am I missing something? Thanks a lot

Comments

  • Admittedly it's a little confusing, but a by-product of improvements to the inventory system from the older releases.

    Esssentially, the InvItem class always refers to the original data - as set in the Inventory Manager. For runtime data, you'll need to access InvInstance instead.

    If you hook into the OnInventorySelect_Alt event instead of OnInventorySelect, that'll give you the associated InvInstance class - with the correct count value.

  • I'm afraid I need further assistance here.
    This is (part of) my original script:

    private void OnEnable()
                {
                    EventManager.OnInventoryDeselect += OnInventoryDeselect;
                    EventManager.OnInventorySelect += OnInventorySelect;
                }
    private void OnDisable()
                {
                    EventManager.OnInventoryDeselect -= OnInventoryDeselect;
                    EventManager.OnInventorySelect += OnInventorySelect;
    
                }
    private void OnInventorySelect(InvItem invItem)
                {
                if (invItem.count > 1)
                                {
                                    AC.GlobalVariables.SetBooleanValue(28, true);
                                }
                }
    

    changing it into

        private void OnEnable()
        {
            EventManager.OnInventoryDeselect += OnInventoryDeselect;
            EventManager.OnInventorySelect += OnInventorySelect_Alt;
        }
    
        private void OnDisable()
        {
            EventManager.OnInventoryDeselect -= OnInventoryDeselect;
            EventManager.OnInventorySelect += OnInventorySelect_Alt;
    
        }
    
        private void OnInventorySelect_Alt(InvItem invItem)
        {
        if (invItem.count > 1)
                        {
                            AC.GlobalVariables.SetBooleanValue(28, true);
                        }
        }
    

    doesn't give any error but invitem.count stills returns 1 as before.

    if doing like this:

        private void OnEnable()
                    {
                        EventManager.OnInventoryDeselect += OnInventoryDeselect;
                        EventManager.OnInventorySelect_Alt += OnInventorySelect;
                    }
    

    I got this error: No overload for 'OnInventorySelect' matches delegate 'EventManager.Delegate_Inventory_Alt'

  • OnInventorySelect_Alt is a separate event with its own parameters - see the link to it above.

    private void OnEnable()
    {
        EventManager.OnInventoryDeselect += OnInventoryDeselect;
        EventManager.OnInventorySelect_Alt += OnInventorySelect;
    }
    
    private void OnDisable()
    {
        EventManager.OnInventoryDeselect -= OnInventoryDeselect;
        EventManager.OnInventorySelect_Alt += OnInventorySelect;
    
    }
    
    private void OnInventorySelect (InvCollection invCollection, InvInstance invInstance)
    {
        if (invInstance.Count > 1)
        {
            AC.GlobalVariables.SetBooleanValue(28, true);
        }
    }
    
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.