Forum rules - please read before posting.

runTimeInventory.hoverItem not Finding Anything in the Menu Inventory Box

edited August 2020 in Technical Q&A

Evening, everybody!

What's tonight's problem? Funny you should ask :-)
For reasons I can't figure, this little script can't seem to any of the items in the inventory it calls.

Here's what everything's built on:
Adventure Creator v. 1.71.7, Unity 2019.4.0f1 and Pixel Crushers' Dialogue System for Unity v 2.2.7

My code, which runs each time an Unity GUI inventory slot is clicked looks like this:

    MenuElement inventorybox = PlayerMenus.GetElementWithName(inventoryName, "InventoryBox");
    // I checked, and "inventoryName" does indeed hold inventory's name at runtime.

    Debug.Log(inventorybox.name); // Returns empty string.
    MenuInventoryBox menuInventoryBox = inventorybox as MenuInventoryBox;
    AC.InvItem selectedItem = AC.KickStarter.runtimeInventory.hoverItem;
    if (selectedItem == null)  // This gets triggered every time, even though the Inventory Box item
                                           // itself had to be visible and clickable for this script to even be 
                                           // called.

        {
        AC.InvItem[] items = AC.KickStarter.runtimeInventory.GetItemsInCategory(inventoryMode);
        selectedItem = items[0];
        Debug.Log("Selected equalled null. Switching to " + selectedItem.label);
        }

I've tried using AC.KickStarter.runtimeInventory.SelectedItem, but that didn't seem to change anything.

Does anybody have any experience with a problem similar to this? How did you overcome it?
Thank you very much for your help!

Comments

  • edited August 2020

    The way the inventory items and menus behave will vary on its properties. Can you share screens of the InventoryBox in the Menu Manager?

    How are you triggering the above code?

    If you're trying to access the inventory item that was clicked on, that's best done by hooking into the OnMenuElementClick custom event, since this contains info about the element and slot that was clicked.

    For example:

    using UnityEngine;
    using AC;
    
    public class RemoveItemsOnEnableMenu : MonoBehaviour
    {
    
        public string inventoryName = "MyInventoryMenu";
    
        private void OnEnable () { EventManager.OnMenuElementClick += OnMenuElementClick; }
        private void OnDisable () { EventManager.OnMenuElementClick -= OnMenuElementClick; }
    
        private void OnMenuElementClick (Menu menu, MenuElement element, int slot, int mouseButton)
        {
            if (menu.title == inventoryName && element.title == "InventoryBox")
            {
                MenuInventoryBox inventoryBox = element as MenuInventoryBox;
                InvItem clickedItem = inventoryBox.GetItem (slot);
            }
        }
    
    }
    
  • Can you share screens of the InventoryBox in the Menu Manager?

    Sure can!

    https://www.mediafire.com/view/5uceuktq5qhjygh/InventoryBox.png/file

    How are you triggering the above code?

    It's triggered via the GUI slots' Button/OnClick doodads in the inspector.

    https://www.mediafire.com/view/n4l254q41t8gdl1/SlotButtonInspector.png/file

    I tried using the script you provided, but it only seems to trigger when the inventory is enabled, and not when the slot buttons are pressed.

    I hope this info helps.
    Thank you again!

  • it only seems to trigger when the inventory is enabled, and not when the slot buttons are pressed.

    How are you determining that? The event should only kick in when clicking on an element - try unsetting the OnClick events and keeping the new script on a separate empty GameObject.

    I'm not clear on what it is you're ultimately trying to achieve here. If you can elaborate more on the intent, I can see if a different approach may be necessary.

  • edited August 2020

    How are you determining that?

    I added a Debug.Log command to the OnMenuElementClicked() Method, and
    it only prints to the console once when the inventory is turned on, never when the
    inventory slots are clicked.

    private void OnMenuElementClick(Menu menu, MenuElement element, int slot, int mouseButton)
    {
    Debug.Log("Running OnMenuElementClick()!!!!!!!!!!!!!!!!");
    if (menu.title == menuName && element.title == "InventoryBox")
    {
    MenuInventoryBox inventoryBox = element as MenuInventoryBox;
    InvItem clickedItem = inventoryBox.GetItem(slot);
    // update the inventory variables in DS and AC
    PixelCrushers.DialogueSystem.DialogueLua.SetVariable("Presented_Item", clickedItem.label);
    AC.GlobalVariables.SetStringValue(24, clickedItem.label); // Updating AC's
    //Presented_Item
    if (clickedItem != null)
    {
    itemCategoryManager.nameDisplay.text = clickedItem.label;
    @Updating the display." + clickedItem.label + " was selected.");
    string description = clickedItem.GetProperty(0).GetValue();
    // Assign the description to the UI text-box
    itemCategoryManager.descriptionDisplay.text = description;
    itemCategoryManager.highlightedItem.texture = clickedItem.tex;
    if (clickedItem.tex == null) { Debug.Log("tex = null!"); }
    if (itemCategoryManager.highlightedItem.texture == null) { Debug.Log("highlightedItem.texture = null!"); }
    }
    else
    {
    Debug.Log("That button's item wasn't found.");
    }
    }
    }

    I'll go ahead and try what you suggested, and tell you what happens.

    I'm not clear on what it is you're ultimately trying to achieve here. If you can elaborate more on the intent, I can see if a different approach may be necessary.

    This menu's purpose is to show the player their inventory, display the name, description and enlarged version of the icon of whichever item they click, and when they press "Present", update a Dialogue System variable to the last chosen item's name, which the game will use to determine conversation flow.

    Besides a backdrop and the "Present" button, the inventory GUI consists of the Inventory Box, a raw image, and two TextMeshProUGUI objects.

    Edit: Sorry about my code insert looking so weird. I couldn't get the Code format to work this time. :-S

  • edited August 2020

    This menu's purpose is to show the player their inventory, display the name, description and enlarged version of the icon of whichever item they click

    So at this stage, you're just looking to extract the details of the item that the player clicks on?

    Debug.Log("Running OnMenuElementClick()!!!!!!!!!!!!!!!!");

    The event itself is called whenever any AC menu is clicked on. If you place this as the first line in the event function, it'll run every time the event is fired. You need to place it after the checks made on the menu and element titles, so that it only gets called when the menu and element we're interested in is clicked on.

    If it's then not running, check that the titles in the script correctly match those in the Menu Manager.

    Going back to your original method though, you should be able to get the clicked item using Unity's own OnClick handler - provided the function it calls recieves the slot index number.

    Try this as a replacement function for your code in the first post:

    public void ClickItemButton (int slot)
    {
        MenuElement inventorybox = PlayerMenus.GetElementWithName(inventoryName, "InventoryBox");
        MenuInventoryBox menuInventoryBox = inventorybox as MenuInventoryBox;
        InvItem invItem = menuInventoryBox.GetItem (slot);
    }
    

    Then, for each of the UI Buttons, have them call this "ClickItemButton" function - but pass the correct slot index number as a parameter. So the first Button would set it to 0, the second to 1, etc.

  • edited August 2020

    Try this as a replacement function for your code in the first post:

    public void ClickItemButton (int slot) { MenuElement inventorybox = PlayerMenus.GetElementWithName(inventoryName, "InventoryBox"); MenuInventoryBox menuInventoryBox = inventorybox as MenuInventoryBox; InvItem invItem = menuInventoryBox.GetItem (slot); }
    What's with the code format? Ah, well....

    This works! Oh, man, what a relief!
    I might make it through this project in one piece yet!

    Thank you so much, and see you next newbie goof!

  • About the code formatting: you just need to give it all a single tab indent before pasting it in here.

  • About the code formatting: you just need to give it all a single tab indent before pasting it in here.

    Ohhhh. Thank you for your help!

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.