Forum rules - please read before posting.

Using Right Click to Examine Inventory Items and Display Close-Up

edited June 2023 in Technical Q&A

Hi there,

I found the code below from The Chamber Demo: https://adventurecreator.org/demo/the-chamber. I modified the code and its setup. I created two menus: one that shows what I just picked up for 1 second and automatically disappears, and another menu that should display a close-up image of the inventory item when I right-click on it (Examine interaction).

I tried using the code below, but the problem is that when I right-click (Examine) an item in the inventory, the ItemGraphic refers to the recently added item instead of the item I right-clicked on. I tried using OnInventoryInteract and OnInventorySelect instead of OnInventoryAdd, but it either gives me an error message or simply doesn't update the graphic on the menu. I would be really grateful if anyone could guide me with correct references or provide alternative suggestions. Thank you so much

using UnityEngine;

namespace AC.TheChamber
{

    public class NewItemMenuSetup : MonoBehaviour
    {

        #region Variables

        private const string menuName = "NewItem";
        private const string inventoryMenuName = "Inventory";
        private const string nameElement = "ItemName";
        private const string graphicElement = "ItemGraphic";

        #endregion


        #region UnityStandards

        private void OnEnable ()
        {
            EventManager.OnInventoryAdd += OnInventoryAdd;
            EventManager.OnMenuTurnOn += OnMenuTurnOn;
            EventManager.OnMenuTurnOff += OnMenuTurnOff;
        }

        private void OnDisable ()
        {
            EventManager.OnInventoryAdd -= OnInventoryAdd;
            EventManager.OnMenuTurnOn -= OnMenuTurnOn;
            EventManager.OnMenuTurnOff -= OnMenuTurnOff;
        }

        #endregion


        #region CustomEvents

        private void OnInventoryAdd (InvItem invItem, int count)
        {
            Menu newItemMenu = PlayerMenus.GetMenuWithName (menuName);

            MenuLabel itemNameLabel = newItemMenu.GetElementWithName (nameElement) as MenuLabel;
            itemNameLabel.label = invItem.GetLabel (Options.GetLanguage ());
            itemNameLabel.UpdateLabelText ();

            MenuGraphic itemGraphic = newItemMenu.GetElementWithName (graphicElement) as MenuGraphic;
            itemGraphic.SetNormalGraphicTexture (invItem.tex);

            newItemMenu.TurnOn ();
        }


        private void OnMenuTurnOn (Menu menu, bool isInstant)
        {
            if (menu.title == menuName)
            {
                PlayerMenus.GetMenuWithName (inventoryMenuName).ignoreMouseClicks = true;
                KickStarter.playerInput.dragOverrideInput = "DragOverride";
                KickStarter.stateHandler.SetInteractionSystem (false);
            }
        }


        private void OnMenuTurnOff (Menu menu, bool isInstant)
        {
            if (menu.title == menuName)
            {
                PlayerMenus.GetMenuWithName (inventoryMenuName).ignoreMouseClicks = false;
                KickStarter.playerInput.dragOverrideInput = string.Empty;
                KickStarter.stateHandler.SetInteractionSystem (true);
            }
        }

        #endregion

    }

}

Comments

  • The script is intended only for the "new item" menu. If you want to make modifications for an "examine" menu, be sure to use a copy so that it doesn't affect the original menu's behaviour.

    Because it's intended to be for the "new item" menu, ItemGraphic is set when the Player picks up a new Inventor item - by hooking into the OnInventoryAdd event.

    You could try using OnInventoryInteract, but this event uses different parameters to OnInventoryAdd - you'll need to update the function's parameters to go along with it:

    OnInventoryInteract (InvInstance invInstance, int iconID)
    

    This will run when an item is interacted with in any way - so you'll need to compare the "iconID" parameter with your examine icon ID if you want to filter it just for examine interactions.

  • Thank you so much again! It works perfectly

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.