Forum rules - please read before posting.

Issue with HoverInstance only reflecting the previously hovered item in an inventory

Hi, I'm trying to use the currently hovered item to propagate an item description and image box.
I'm using direct controlled menus to navigate a 3 by 3 grid (i've posted before here: https://imgur.com/EfzTJwl & https://imgur.com/a/zwH9MwF in other threads)

I am still having a few issues - the first of which is that if I set the debug log to print the label (since I'm referring to the item instance, it is GetFullLabel - correct?) unfortunately it will print the name of the item I have just moved the selection box off, instead of the one I'm currently over. It's like the point at which it updates is only once a new item is hovered!

Next, if I select the item, which opens a second menu with options to use/discard etc, once I return to the inventory box, nothing will read as being hovered over at all - somehow the OnMouseOverMenu event just doesn't get called anymore.

The third issue is that I cannot for the life of me figure out how to link an image component of a prefab to the UI and have it update on hovering since I can't access the linked prefab of an inventory item if I'm using invinstance. If I use Invitem, it just doesn't detect anything as being present in the inventory during the OnMouseOverMenu event.

What would be the best way to load a UI image from the prefab of an item? I figured I could simply swap an image component in my UI with the image linked to the prefab, but I can't seem to get it working. The other option you suggested in another thread would be to load a resource using an item's string property, but how do I turn a string variable into an actual function I can call through unity's event system?

Sorry if those are n00b questions!

Comments

  • the first of which is that if I set the debug log to print the label (since I'm referring to the item instance, it is GetFullLabel - correct?

    GetFullLabel will include verb names as well as the item (i.e. "Use X" instead of just "X") depending on your settings. If you just want the item's name, use InvItem.GetLabel.

    Which item it gets will depend on how and when you're calling it. Can you share the code and event you're using to call it?

    once I return to the inventory box, nothing will read as being hovered over at all - somehow the OnMouseOverMenu event just doesn't get called anymore.

    Is this for a Unity UI-based menu? Are you using a custom EventSystem prefab, and does the menu otherwise behave the same?

    Place a Debug.Log statement in your OnMouseOverMenu event and share the stacktrace so we can see where it's being called from.

    The third issue is that I cannot for the life of me figure out how to link an image component of a prefab to the UI and have it update on hovering since I can't access the linked prefab of an inventory item if I'm using invinstance.

    In what way are you trying to update the image? InvInstance has an InvItem property that gives you a reference to the original InvItem class.

    What would be the best way to load a UI image from the prefab of an item?

    If you know the item, or InvInstance, you can get it's texture with:

    invInstance.InvItem.tex
    

    This can't be applied to an Image component directly (you have to convert it to a Sprite), but you can apply it to a RawImage component as that accepts Texture assets.

  • edited April 2021

    Which item it gets will depend on how and when you're calling it. Can you share the code and event you're using to call it?

    private void OnEnable() 
    {
        EventManager.OnMouseOverMenu += OnMouseOverMenu;
    
    }
    private void OnDisable() 
    {
        EventManager.OnMouseOverMenu -= OnMouseOverMenu;
    
    }
    
    
    
    private void OnMouseOverMenu(AC.Menu _menu, MenuElement _element, int _slot)
    {
    
        if (_menu.title == "Inventory")
        {
            AC.InvInstance hoverItem = AC.KickStarter.runtimeInventory.HoverInstance;
    
            if (hoverItem != null)
            {
                print("Hovering over" + hoverItem.GetFullLabel());
            }
    

    Should just note, that if I create a menu element in the Inventory menu, that is a label set to display the name of the Hovered Item - it works just fine. So I'm assuming I'm doing something wrong, and referencing the previously hovered item somehow?

    Is this for a Unity UI-based menu? Are you using a custom EventSystem prefab, and does the menu otherwise behave the same?

    EDIT - I posted a bunch of code that works my little interaction menu and in doing so, figured out that I'd left in a line that made the inventorybox element unclickable! So that's fixed at least, in that it displays the hovered items label both before and after opening the interaction menu.

    If you know the item, or InvInstance, you can get it's texture with: invInstance.InvItem.tex
    This can't be applied to an Image component directly (you have to convert it to a Sprite), but you can apply it to a RawImage component as that accepts Texture assets.

    I'm hoping to use an entirely different image for the "close up" picture than the icon I use in the inventory boxes. So far, I've got one image for the "unlclicked" item, then another as its active/selected image - and was wanting a third one for its closeup. I was tossing up using the active/selected and simply displaying whichever I'm not using for when it's selected in the inventory box - but I saw that you can add a prefab to each item, only I can't figure out how to reference the linked prefab since InvInstance doesn't reference it directly. How would I go about referencing the original Invitem class to then reference the Prefab?

    Something like this (added to the Image component on the UI)?

    public void imageSwap()
    {
        Texture bigImage = GetComponent<RawImage>().texture;
        AC.InvInstance selectedItem = AC.KickStarter.runtimeInventory.HighlightInstance;
    
        if (selectedItem != null)
        {
            print("Hovering over" + selectedItem.GetFullLabel());
            bigImage = selectedItem.InvItem.linkedPrefab.GetComponent<RawImage>().texture;
        }
    }
    
  • edited April 2021

    AC.InvInstance hoverItem = AC.KickStarter.runtimeInventory.HoverInstance;

    As AC's HoverInstance property is updated as part of the internal update cycle, it may take another frame to reflect the "true" state of things depending on how you reference it.

    When hooking into the OnMouseOverMenu event though, you can read the event's slot parameter to get the live value instead:

    if (_element != null && _element == MenuInventoryBox)
    {
        InvInstance hoverInstance = (_element as MenuInventoryBox).GetInstance (_slot);
    }
    

    How would I go about referencing the original Invitem class to then reference the Prefab?

    InvInstance has an InvItem property, which contains the prefab reference:

    hoverInstance.InvItem.linkedPrefab
    

    I'm hoping to use an entirely different image for the "close up" picture than the icon I use in the inventory boxes

    There's a couple of ways you can reference a separate image asset:

    1) Use a linked prefab as you mentioned, with each prefab having a custom MonoBehaviour script that has a public Sprite variable that you then extract, i.e.:

    Sprite sprite = hoverInstance.InvItem.linkedPrefab.GetComponent <ItemPrefabData>().mySprite;
    

    2) Define a String inventory property that refers to the sprite asset by name, and then retrieve it via Resources.Load, i.e.:

    Sprite sprite = (Sprite) Resources.Load (hoverInstance.GetProperty ("SpriteName").TextValue);
    
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.