Forum rules - please read before posting.

Selecting inventory item in UI

edited May 14 in Technical Q&A

Hello I want to put a highlight selected inventory item from UI. This is the relevant code:

private void Update()
        {
            var item = KickStarter.runtimeInventory.SelectedItem;
            if (item != null)
            {
                lastSelectedItem = item;
                KickStarter.runtimeInventory.SelectItem(null); //I deselect this to discard dragging mechanic of the AC
                HighlightItem(lastSelectedItem);
            }
        }
        private void HighlightItem(InvItem selectedItem)
        {

    }

Comments

  • Welcome to the community, @jigsaw.

    In what way do you want to highlight it? With a texture swap?

    If you read SelectedInstance instead, you'll get a reference to the selected item's Instance, which has a Tex property you can use to override its texture:

    private void Update()
    {
        var instance = KickStarter.runtimeInventory.SelectedInstance;
        if (InvInstance.IsValid (item))
        {
            lastSelectedItem = item;
            KickStarter.runtimeInventory.SelectItem(null); //I deselect this to discard dragging mechanic of the AC
            HighlightItem(lastSelectedItem);
        }
    }
    private void HighlightItem(InvInstance selectedItem)
    {
        selectedItem.Tex = newTexture;
    }
    

    If you can share more context about the situation, I may be able to suggest a more specific approach.

  • Hi sorry for insufficient information. I want to add a custom sprite (a game object) on selected item. Imagine a frame sprite on inventory item. The problem is I cannot find the position of the selected inventory item (from UI).

  • edited May 14

    When dealing with UI, it's better to attach your script to each Button in the UI prefab, and then compare the "instance" variable with that associated with the Button it's attached to:

    public bool IsThisButton (InvInstance invInstance)
    {
        var inventory = PlayerMenus.GetElementWithName ("Inventory", "Items") as MenuInventoryBox;
        int slotIndex = inventory.GetSlotIndex (gameObject);
        var thisInvInstance = inventory.GetInstance (slotIndex);
        return thisInvInstance == invInstance;
    }
    

    Replacing "Inventory" and "Items" with the names of your Inventoy menu, and the InventoryBox element inside it, respectively.

  • I couldn't make this work with invInstance. I went changing texture way:

     private void HighlightItem(InvItem selectedItem)
     {
            RestoreAllTex();
            selectedItem.tex = selectedItem.selectedTex;
     }
     private void OnDestroy()
     {
            RestoreAllTex(); //for some reason it is not reset to default tex
     }
     private void RestoreAllTex()
     {
            int numberOfItemsCarried = KickStarter.runtimeInventory.GetNumberOfItemsCarried();
            for (int i = 0; i < numberOfItemsCarried; i++)
            {
                var inventoryItem = KickStarter.runtimeInventory.GetItem(i);
                inventoryItem.tex = inventoryItem.activeTex;
            }
    }
    
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.