Forum rules - please read before posting.

Automatically Close Inventory Menu After Activating It With a Button

2»

Comments

  • Hello everyone, I was having the same problem as others here before with a twist.
    Currently my Inventory is being opened by scrolling down and closed by scrolling up. Using this little script I put together.

    void Update()
    {
        if (Input.mouseScrollDelta.y < 0)
        {
            PlayerMenus.GetMenuWithName("Inventory").TurnOn();
        }
        if (Input.mouseScrollDelta.y > 0)
        {
            PlayerMenus.GetMenuWithName("Inventory").TurnOff();
        }
    }
    

    For this to work I have my Inventory Appear Type set to manual. But closing it by scrolling just doesnt feel very intuitive and immersive. That's why I would also like it to be closed when the mouse leaves the area.

    I tried some of the steps mentioned before and will continue to do so, but it seems like a fairly different scenario than the previous ones, so I leave this here.

    Any help would be greatly appreciated.

  • Welcome to the community, @Eldakan.

    You can determine if the mouse is over a menu with:

    PlayerMenus.GetMenuWithName (myMenuName).IsPointInside (KickStarter.playerInput.GetInvertedMouse ());
    

    Your script would probably need updating to something like:

    Menu inventoryMenu;
    void Update()
    {
        if (inventoryMenu == null) inventoryMenu = PlayerMenus.GetMenuWithName ("Inventory");
    
        if (inventoryMenu.IsOn ())
        {
            if (inventoryMenu.IsPointInside (KickStarter.playerInput.GetInvertedMouse ()))
            {
                inventoryMenu.TurnOff ();
            }
        }
        else
        {
            if (Input.mouseScrollDelta.y < 0)
            {
                inventoryMenu.TurnOn ();
            }
        }
    }
    
  • thanks for your help and the welcome @ChrisIceBox!

    I figured something out now using the function you gave me.

    I didnt use the script you sent, because I am fairly new to C# and scripting in general so I didnt understand it well enough.

    I used an other menu called InventoryOff and a boolean called InMenu and this script (for future help perhaps):

      if (PlayerMenus.GetMenuWithName("Inventory").IsVisible())
            {
                if (PlayerMenus.GetMenuWithName("Inventory").IsPointInside(KickStarter.playerInput.GetInvertedMouse()))
                {
                     InMenu = true;
                }
            }
       if (InMenu)
            {
              if    (PlayerMenus.GetMenuWithName("InventoryOff").IsPointInside(KickStarter.playerInput.GetInvertedMouse()))
                {
                    PlayerMenus.GetMenuWithName("Inventory").TurnOff();
                    InMenu = false;
                }
    }
    
  • I think my code above was flawed, as it wouldn't account for the mouse not initially being over the menu when first turned on. If what you have works, stick to it - but my corrected code is here:

    Menu inventoryMenu;
    bool isInsideMenu;
    void Update()
    {
        if (inventoryMenu == null) inventoryMenu = PlayerMenus.GetMenuWithName ("Inventory");
    
        if (inventoryMenu.IsOn ())
        {
            if (inventoryMenu.IsPointInside (KickStarter.playerInput.GetInvertedMouse ()))
            {
                isInsideMenu = true;
            }
            else if (isInsideMenu)
            {
                isInsideMenu = false;
                inventoryMenu.TurnOff ();
            }
        }
        else
        {
            if (Input.mouseScrollDelta.y < 0)
            {
                inventoryMenu.TurnOn ();
            }
        }
    }
    
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.