Forum rules - please read before posting.

Turn off the inventory menu automatically when an item is being dragged

Hi,
maybe this is a stupid question, but I really don't know how to do this: Is it possible to turn off the inventory automatically when an item is picked up and being dragged with the the drag and drop functionality on. Or even better to turn off the inventory when an item is being dragged outside the inventory texture?

Do I have to make a custom script for it(if so please help me!!!) or there is a way already in the visual UI?

Thank you very much,

Comments

  • If the Menu's Appear type property is set to Mouse Over, then it will turn itself off automatically once the cursor leaves its boundary.

    This boundary is for the whole Menu, however. If you want to turn the Menu off at the moment an item is selected - ignoring the cursor's position - then you'll need to use a simple script.

    What is the "Appear type" currently set to? If it's set to Manual, you can turn it off upon selection with this script (HideMenuOnItemSelect.cs):

    using AC;
    using UnityEngine;
    
    public class HideMenuOnSelectItem : MonoBehaviour
    {
    
        public string menuName = "Inventory";
    
        private void OnEnable () { EventManager.OnInventorySelect += OnInventorySelect; }
        private void OnDisable () { EventManager.OnInventorySelect -= OnInventorySelect; }
    
        private void OnInventorySelect (InvItem invItem)
        {
            PlayerMenus.GetMenuWithName (menuName).TurnOff ();
        }
    
    }
    

    If the Menu's appearance is rule-based, however, e.g. During Gameplay, then you'll instead want the script to lock it. When a Menu is locked, it will not display even when its appear-type condition is met:

    using AC;
    using UnityEngine;
    
    public class HideMenuOnSelectItem : MonoBehaviour
    {
    
        public string menuName = "Inventory";
    
        private void OnEnable () { EventManager.OnInventorySelect += OnInventorySelect; }
        private void OnDisable () { EventManager.OnInventorySelect -= OnInventorySelect; }
    
        private void OnInventorySelect (InvItem invItem)
        {
            PlayerMenus.GetMenuWithName (menuName).isLocked = true;
        }
    
    }
    

    You can turn on / unlock the Menu using the Menu: Change state Action - or by extending the script. That will, however, depend on when you want to do so. If you can share details of the exact behaviour you're looking for, I can advise further.

  • Hi Chris, thank you very much for your quick reply.

    The inventory appear type is now set on input key. The ideal behaviour would be:
    1-The player presses the assigned key to open the inventory menu.
    2-The inventory texture, representing a big pouch, appears at the center of the screen.
    3-The player picks up an item carries it outside the boundaries of the inventory texture, thanks to that, the inventory turns itself off.

    Thanks a lot again,

  • Try this:

    using UnityEngine;
    using AC;
    
    public class HideMenuOnSelectItem : MonoBehaviour
    {
    
        public string menuName;
    
        private void Update ()
        {
            Menu menu = PlayerMenus.GetMenuWithName (menuName);
            Vector2 invertedMouse = KickStarter.playerInput.GetInvertedMouse ();
            if (menu.IsVisible () && !menu.IsPointInside (invertedMouse) && KickStarter.runtimeInventory.SelectedItem != null)
            {
                menu.TurnOff ();
            }
        }
    
    }
    
  • I put the script in the inventory folder. It compiled, but unfortunately nothing happened. When I open up the inventory and drag an item outside its boundaries, nothing happens, the inventory remains there. Strangely, even with your previous script, HideMenuOnSelectItem you wrote before, nothing happens also when I select the item. The inventory stays on.

  • edited September 2022

    It worked! I added public string menuName = "Inventory" to the second script. I forgot the = and it was getting ; as menuName.

    Stupid me!!!(I'm learning...)

    Thank you very much!!! Have a lovely day!!

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.