Forum rules - please read before posting.

How to make Inventory Items auto-deselect other items on click?

Hi everyone,

I am facing a problem with item selection inside the Inventory UI I'm trying to build. I would like to deselect previously selected items with a single mouse click instead of two. Like swapping between them instead of having to click twice (one for deselection and then to select the new item).

This is the behavior I would like to have and I've been able to set it up only for the Objectives:

here are the settings:

This is instead what happens with the Inventory: when I want to select a new item it firstly needs to deselect the current one

again, the settings:

I am using Unity v.2020.3.34f1 and AC v.1.75.4

Any help would be greatly appreciated.
Thank you

Comments

  • Welcome to the community, @Viro.

    When an item is selected, the default behaviour when clicking another item is to attempt to combine the two, and then deselect the original item. You've prevented the cursor from changing to show the selected item, but this behaviour is still in force.

    It's possible to use custom scripting to modify the behaviour of Inventory menus to suit your needs, however. If you go to your Items element and set its Inventory box type property to Custom Script, you'll then be able to control its click behaviour entirely through script.

    The best way to do this is to hook into the OnMenuElementClick custom event, which is triggered whenever the user clicks on a menu. More on this topic can be found in the Manual's "Custom interaction systems" chapter, but this script placed on the UI prefab should do it:

    using UnityEngine;
    using AC;
    
    public class SingleSelectItems : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnMenuElementClick += OnMenuElementClick; }
        private void OnDisable () { EventManager.OnMenuElementClick -= OnMenuElementClick; }
    
        private void OnMenuElementClick (AC.Menu menu, MenuElement element, int slot, int buttonPressed)
        {
            if (menu.title != "Inventario" || element.title != "Items")
            {
                return;
            }
    
            MenuInventoryBox inventoryBox = element as MenuInventoryBox;
            InvInstance clickedInstance = inventoryBox.GetInstance (slot);
    
            if (InvInstance.IsValid (clickedInstance))
            {
                clickedInstance.Select ();
            }
        }
    
    }
    
  • Thank you Chris for the super-quick reply.

    It works perfectly but now the items' images are no longer showing up when the items are clicked. I was relying on another InventoryBox of type "Display Selected" showing "Only Icons" - as mentioned in the manual - and I can imagine that's happening because the behaviour of the click has been changed. Unfortunately, I'm still quite a newbie at scripting :# but I'll eventually get better at it.

  • Try adding the following underneath the "clickedInstance.Select ();" line:

    PlayerMenus.ResetInventoryBoxes ();
    
  • Unfortunately, I've noticed something new. The new click behaviour is triggered even if I only switch from "InventoryBox type: Default" to "InventoryBox type: Custom script" without attaching any script.

    I instead get a warning when I attach the script to the prefab saying that it can't be loaded:

    And another one appears if I simply try to move something inside the prefab telling me that the script does not derive from MonoBehaviour:

    I'm really sorry to bother you again with things that mainly happen because I'm still learning

  • The issues you're getting are from how Unity works - a component's filename must match the class name.

    The script class above is named SingleSelectItems - so the filename will need to be SingleSelectItems.cs.

    If you want to create a new component named MenuClickBehaviour that uses the above code, just replace:

    public class SingleSelectItems : MonoBehaviour
    

    with:

    public class MenuClickBehaviour : MonoBehaviour
    
  • Oh Lord, I feel so dumb... Thank you so much! And thank you from both Adventure Creator and your helpfulness, the first is an AMAZING tool (I hope to improve eventually my understanding of the coding aspect haha) and the latter is the cherry on top!

    Again, thank you!

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.