Forum rules - please read before posting.

Action when Item is in Inventory?

edited June 2022 in Technical Q&A

When I have a specific item in inventory, I want to turn on a special Menu. What is the best approach to doing it? I need an action that permanently checks inventory for the item.

A workaround would be using variables and cutscene on variable change but maybe there is an easier way?

Comments

  • There are two approaches you can take: one using script, another using ActionList parameters.

    Through script, you can hook into the OnInventoryAdd / OnInventoryRemove custom events to check if a given item is present, and control a Menu accordingly (either turning it on/off or locking/unlocking it).

    For example:

    using AC;
    using UnityEngine;
    
    public class UnlockMenuWithItem : MonoBehaviour
    {
    
        public int itemID;
        public string menuName;
    
        private void OnEnable ()
        {
            EventManager.OnInventoryAdd += OnInventoryUpdate;
            EventManager.OnInventoryRemove += OnInventoryUpdate;
        }
    
        private void OnDisable ()
        {
            EventManager.OnInventoryAdd -= OnInventoryUpdate;
            EventManager.OnInventoryRemove -= OnInventoryUpdate;
        }
    
        private void OnInventoryUpdate (InvItem invItem, int amount)
        {
            bool showMenu = KickStarter.runtimeInventory.PlayerInvCollection.Contains (itemID);
            PlayerMenus.GetMenuWithName (menuName).isLocked = !showMenu;
        }
    
    }
    

    With parameters, you could create a pair of ActionList assets that are used to add and remove an inventory item respectively using the Inventory: Add or remove Action, with both Actions having their "Item to add/remove" fields overridden with an Inventory item parameter.

    Following these Actions, you could then run an Inventory: Check Action to check if the given item is being carried by the Player, and then update a Menu accordingly.

    You'd then want to run these ActionList assets using the ActionList: Run Action whenever you wanted to add or remove an item, instead of using the Inventory: Add or remove Action directly.

    A tutorial on ActionList parameters can be found here.

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.