Forum rules - please read before posting.

Retrieving inventory box current slot via script

Unity 2018.3.1f1 AC 1.71.8

Apologies in advance, I'm sure this is just down to my feeble coding knowledge but I can't see where I'm going wrong here.

I'm trying to retrieve the currently highlighted slot of an inventorybox and store it in a global int value.

Here's my code, it's saying unable to convert InvItem to Int on that last line:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using AC;

public class GetCurrentInvSlot : MonoBehaviour
{

private void Update()
{
    MenuInventoryBox inventoryBox = PlayerMenus.GetElementWithName("Inventory", "InventoryBox") as AC.MenuInventoryBox;
    InvItem invItem = inventoryBox.GetItem(1); // Get 2nd slot item (starting index 0)
    GlobalVariables.SetIntegerValue(82, invItem);
}

}

Comments

  • After a lot of head scratching I resolved the issue and decided to get the inventory item by label in the process:

    MenuInventoryBox inventoryBox = PlayerMenus.GetElementWithName("Inventory", "InventoryBox") as AC.MenuInventoryBox;
    InvItem myItem = inventoryBox.GetItem(0); // Get 2nd slot item (starting index 0)
    string ItemName = myItem.label;
    GlobalVariables.SetStringValue(81, ItemName);

    In case anyone ends up reading this, the extra line:
    'string ItemName = myItem.label;'

    fixed the issue, if you still need the ID you would use:

    'int ItemID = myItem.id;'

  • Are you referring to the item being hovered over by the cursor?

    If so, it's possible to read:

    AC.KickStarter.runtimeInventory.hoverItem
    

    instead of having to access the MenuInventoryBox element that's displaying it.

  • I'm using keyboard input for my menus so hoverItem wasn't suitable here. I probably should have mentioned that! :)

  • In that case, you should still be able to hook into the OnMouseOverMenu custom event - which still works with direct control:

    using UnityEngine;
    using AC;
    
    public class GetHoverItem : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnMouseOverMenu += OnMouseOverMenu; }
        private void OnDisable () { EventManager.OnMouseOverMenu -= OnMouseOverMenu; }
    
        private void OnMouseOverMenu (Menu menu, MenuElement element, int slot)
        {
            if (menu.title == "Inventory" && element != null && element.title == "InventoryBox")
            {
                MenuInventoryBox inventoryBox = element as MenuInventoryBox;
                InvItem item = inventoryBox.GetItem (slot);
            }
        }
    
    }
    
  • Bringing back this topic...
    For some reasons the "OnMouseOverMenu" doesn't work on my case when I'm directly navigating a special inventory (via GamePad or Keyboard keys).

    So I wonder if it's possible via custom script to get the item of an InventoryBox using the Unity Event "OnSelect" (ISelectHandler).

    Basically, I'd like to get which slot (and therefore the item on that slot) of the InventoryBox I'm currenting selecting (with "selecting" I mean a UnityEngine.UI button select).

    The script is attached to each Button of the InventoryBox.

    Any clues?

  • The code above could be adapated to work with OnSelect - you'd just need to expose the variables so that you can set them in the Inspector, i.e.:

    using UnityEngine;
    using UnityEngine.EventSystems;
    using AC;
    
    public class GetHoverItem : MonoBehaviour, ISelectHandler,
    {
    
        public string menuName = "Inventory";
        public string elementName = "InventoryBox";
        public int slot;
    
        public void OnSelect(BaseEventData eventData)
        {
            MenuInventoryBox inventoryBox = PlayerMenus.GetElementWithName (menuName, elementName) as MenuInventoryBox;
            InvItem item = inventoryBox.GetItem (slot);
        }
    
    }
    

    OnMouseOverMenu still ought to work when using direct-control, however. Have you enabled direct-navigation in the Menu Manager, and using AC's provided Event System?

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.