Forum rules - please read before posting.

Help with 2d game Inventory systems

Hey!

I have few questions related to inventory.

Question 1.
I have inventory that opens from a button in the top corner. When pressed inventory opens the center of the screen. I want it to close by clicking anything from outside the inventory screen.

Question 2.
I also have 3 buttons in that inventory: Use, Examine and Combine, what kind of action list I should put them to work correctly?

Question3.
I have an icon of Bag as an inventory button, how I can make it change the graphic, (bag open) when inventory is open?

Thanks already :)

Comments

  • I want it to close by clicking anything from outside the inventory screen

    A quick-and-dirty way would be to make the inventory menu full-screen, and have invisible buttons cover the outside - and clicking any of these buttons causes it to close.

    The "proper" way would be to use a custom script that closes the menu (here named "Inventory") if it detects a mouse click while the cursor is in empty space:

    using UnityEngine;
    using AC;
    
    public class ClickOffInventory : MonoBehaviour
    {
    
        void Update ()
        {
            if (Input.GetMouseButtonDown (0) && !KickStarter.playerMenus.IsMouseOverMenu () && PlayerMenus.GetMenuWithName ("Inventory").IsOn ())
            {
                PlayerMenus.GetMenuWithName ("Inventory").TurnOff ();
            }
        }
    
    }
    

    Attach to a new GameObject in the scene to have it close a menu named "Inventory".

    I also have 3 buttons in that inventory: Use, Examine and Combine, what kind of action list I should put them to work correctly?

    It depends on how you want to use those buttons. Click "Use" and then click an item to use, or the other way around?

    It also depends on your chosen "Interaction method" - what have you got this set to in your Settings Manager?

    I have an icon of Bag as an inventory button, how I can make it change the graphic, (bag open) when inventory is open?

    If you had the "bag open" icon as a separate Button/element in your Menu, you could use the Menu: Change state Action in the Inventory menu's ActionList when turn on asset to show it using the Show Menu Element command, and then hide it with another such Action in the Inventory menu's ActionList when turn off asset.

    Alternatively, you can use scripting to handle it. The OnMenuTurnOn/Off custom events can be used to update a Unity UI-based Menu Button's appearance when the Inventory menu is turned on/off:

    using UnityEngine;
    using UnityEngine.UI;
    using AC;
    
    public class InventoryBagIcon : MonoBehaviour
    {
    
        public Image bagImage;
        public Sprite openSprite;
        public Sprite closedSprite;
    
        private void OnEnable ()
        {
            EventManager.OnMenuTurnOn += OnMenuTurnOn;
            EventManager.OnMenuTurnOff += OnMenuTurnOff;
        }
    
        private void OnDisable ()
        {
            EventManager.OnMenuTurnOn -= OnMenuTurnOn;
            EventManager.OnMenuTurnOff -= OnMenuTurnOff;
        }
    
        private void OnMenuTurnOn (AC.Menu menu, bool isInstant)
        {
            if (menu.title == "Inventory")
            {
                bagImage.sprite = openSprite;
            }
        }
    
        private void OnMenuTurnOff (AC.Menu menu, bool isInstant)
        {
            if (menu.title == "Inventory")
            {
                bagImage.sprite = closedSprite;
            }
        }
    
    }
    

    You'll need to use Unity UI for your bag icon menu, but attach this to the UI prefab, assign the Image/sprites in its Inspector and it should update its appearance automatically.

  • Hey, Chris Thanks for the answers!

    1. Works good now!

    2. My game mode is context-sensitive, I want first to select an item in the inventory after that Press use which lets me use an item to hotspots, examine the button to tell something about the item, and combine combines it with selected items.

  • In Context Sensitive mode, items will behave like Hotspots - they can be used or examined by defining ActionLists in their properties, and then left/right clicking on the item icons in-game.

    To be able to first select them (so that they become the active cursor) and then use/examine them via a separate menu button, first define the ActionLists as normal.

    You'll need to prevent these Use/Examine ActionLists from running after clicking an item in the Inventory menu. To do this, select your InventoryBox element in the Inventory menu, and check Prevent interactions?.

    Then create your Use/Examine buttons - probably best in the same Inventory menu - and then use a script that hooks into the OnMenuElementClick event to manually interact with an item if one is selected when either of those new buttons are clicked:

    using UnityEngine;
    using AC;
    
    public class CustomInventoryInteractions : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnMenuElementClick += OnMenuElementClick; }
        private void OnDisable () { EventManager.OnMenuElementClick -= OnMenuElementClick; }
    
        void OnMenuElementClick (Menu _menu, MenuElement _element, int _slot, int buttonPressed)
        {
            if (_menu.title == "Inventory" && InvInstance.IsValid (KickStarter.runtimeInventory.SelectedInstance))
            {
                if (_element.title == "Use")
                {
                    KickStarter.runtimeInventory.SelectedInstance.Use ();
                }
                else if (_element.title == "Examine")
                {
                    KickStarter.runtimeInventory.SelectedInstance.Examine ();
                }
            }
        }
    
    }
    

    This script assumes that your Buttons are named Use and Examine, and placed in a menu named Inventory - update the script if it differs.

    Combining items is a bit more complex. The normal way is to first select an item, then click another item. If you want to move this to a separate "Combine" button, what would the exact behaviour be after clicking it?

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.