Forum rules - please read before posting.

Inventory offset

Hi, I'm making a 2D game with the direct movement method using a keyboard.

I have an inventory with 5 slots. I set 2 buttons to offset it 1 slot at a time. Let's say I have 10 items. When the first time I open the inventory, it'll show the first 5 items. If I offset it forward 3 times, for example, it'll now show the items #4-8. When I turn the inventory off and then turn it on again, it's still offset and shows the items #4-8. Is it possible to make it show the first 5 items like the first time I opened it?

Thanks a lot!

Comments

  • Yes - though you'll need a custom script to do so, as it involves calling the InventoryBox element's ResetOffset function as the Menu turns on.

    This should do it:

    using UnityEngine;
    using AC;
    
    public class RestInventoryOffset : MonoBehaviour
    {
    
        public string menuName = "Inventory";
        public string elementName = "Items";
    
        void OnEnable () { EventManager.OnMenuTurnOn += OnMenuTurnOn; }
        void OnDisable () { EventManager.OnMenuTurnOn -= OnMenuTurnOn; }
    
        void OnMenuTurnOn (Menu _menu, bool isInstant)
        {
            if (_menu.title == menuName)
            {
                (_menu.GetElementWithName (elementName) as MenuInventoryBox).ResetOffset ();
            }
        }
    
    }
    
  • edited August 2024

    Hi, I can't figure out how to make it work. Could you point out where I'm doing wrong?

    I added the script to a gameobject prefab
    https://drive.google.com/file/d/1m4cKqjN--e4ZKmN6OLj70bCNvLUc6-XX/view?usp=sharing

    I used Object: Send message in the Actionlist when turn on the Inventory to call the function. I tried to use it in other places but it didn't work either.
    https://drive.google.com/file/d/1mdbFxhwSDK_FyxOcaiiuJyPVr_xo1mtc/view?usp=sharing

    Here is my Inventory setup.
    https://drive.google.com/file/d/1mDXmCHUzQDNf7QLiKvlBRbYWvx7Kin35/view?usp=sharing

    I also changed the menu name in the script to match the menu.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using AC;
    
    public class RestInventoryOffset : MonoBehaviour
    {
    
        public string menuName = "Inventory";
        public string elementName = "InventoryBox";
    
        void OnEnable() { EventManager.OnMenuTurnOn += OnMenuTurnOn; }
        void OnDisable() { EventManager.OnMenuTurnOn -= OnMenuTurnOn; }
    
        void OnMenuTurnOn(Menu _menu, bool isInstant)
        {
            if (_menu.title == menuName)
            {
                (_menu.GetElementWithName(elementName) as MenuInventoryBox).ResetOffset();
            }
        }
    
    }
    

    Here is the video test
    https://drive.google.com/file/d/1mjGRJKaT1vb7W3LgAXtulJS3qEKABGrQ/view?usp=sharing

  • The script is self-contained: it hooks into the OnMenuTurnOn custom event, which kicks in automatically. You don't need to use Object: Send message.

    The component in your screenshot isn't enabled, however - make sure it's enabled and present in the scene, and it should work.

  • I put the script in the Persistent Engine prefab and it works. Thanks a lot for the help, Chris!

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.