Forum rules - please read before posting.

Inventory item select

So, I currently have my inventory set to drag item, but I would like to try out a different way:

  1. Once the item is selected, the inventory closes, the item stays selected rather than drag, and the item stays selected until right click or the item is used correctly (ie not an unhandled event)

  2. Happy for the cursor to close with drag once the cursor is out of the inventory area

My inventory is animated using ON/OFF etc

Thanks

«1

Comments

  • You can disable drag-and-drop mode from the Settings Manager.

    If Interaction method is set to Context Sensitive, or Inventory interactions is set to Single, then selecting an item with not "Use" interaction defined will cause it to become selected. Further options in the Settings Manager allow you to prevent it from becoming de-selected when left-clicking.

    A script that hooks into the OnInventorySelect event can be used to turn off the Inventory menu when an item is selected:

    using AC;
    using UnityEngine;
    
    public class CloseInventoryOnSelect : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnInventorySelect += OnInventorySelect; }
        private void OnDisable () { EventManager.OnInventorySelect -= OnInventorySelect; }
    
        private void OnInventorySelect (InvItem invItem)
        {
            PlayerMenus.GetMenuWithName ("Inventory").TurnOff ();
        }
    
    }
    
  • edited November 2021

    ok thanks, i will give this a try, can i ask how i would add a line of code to animate a ui prefab menu game object from here also?

    This is the object and animation:

    https://www.dropbox.com/s/go49w4762zq4bvr/animate from script.png?dl=0

  • If you define a Trigger parameter in your Animator that causes the animation to play, you can invoke it through script with:

    GameObject.Find ("Briefcase").GetComponent<Animator>().SetTrigger ("MyTrigger");
    
  • edited November 2021

    could i combine the two script codes with this (this closes the inventory menu when player walks)? And how would be best to add it in?

    using UnityEngine;
    using System.Collections;
    using AC;
    
    
    public class CloseInventory : MonoBehaviour
    {
        void Awake()
        {
            DontDestroyOnLoad(this.gameObject);
    
        }
        void Start()
        {
    
        }
        private void Update()
    
        {
    
            if (KickStarter.player.IsMovingAlongPath())
            {
                PlayerMenus.GetMenuWithName("RealWorldInventory").TurnOff();
                PlayerMenus.GetMenuWithName("SleepytimeVillageInventory").TurnOff();
                PlayerMenus.GetMenuWithName ("InGame").TurnOn();;
                GameObject.Find ("Briefcase").GetComponent<Animator>().SetTrigger ("MyTrigger");
    
            }
    
    
        }
    
    
    }
    
  • I don't think it is currently having effect on the briefcase, is that the right line for the script when it is set in the prefab UI as in screenshot? Also screenshot of trigger set up:

    https://www.dropbox.com/sh/px8takv5aanw24x/AADN49Qwbt9JlUZcSP9yxxJPa?dl=0

  • Replace "MyTrigger" with the name of the actual parameter you want to trigger - in this case, "Close".

  • edited November 2021
    Ah I did that too, but it won’t seem to animate
  • Is the Briefcase object enabled in the Hierarchy at the time?

    Uncheck "Has Exit Time" in the transition, too.

  • The Animator you're showing is for ToyChest, but the script you've posted is for Briefcase, which is disabled at the time of the video.

  • edited November 2021
    Sure, the animation trigger applied to both ToyChest and Briefcase, depending on which scene you are in, my bad for not explaining - In the video I have adapted the code:

    GameObject.Find ("Briefcase").GetComponent<Animator>().SetTrigger ("Close");
    GameObject.Find ("ToyChest").GetComponent<Animator>().SetTrigger ("Close");

    But it does still not close on player walk
  • I have sorted this now thanks!

  • How would I used the script above in scene?

  • The script above causes the object it's attached to to survive scene changes, making it scene-independent.

    If you wanted it to work in a single scene, remove its Awake function.

  • ok thanks! How would i change this script to deselect the item once the player moves?

    using UnityEngine;
    using System.Collections;
    using AC;

    public class CloseInventory : MonoBehaviour
    {
    void Awake()
    {
    DontDestroyOnLoad(this.gameObject);

    }
    void Start()
    {

    }
    private void Update()
    

    {

    if (KickStarter.player.IsMovingAlongPath())
    {
    PlayerMenus.GetMenuWithName("RealWorldInventory").TurnOff();
    PlayerMenus.GetMenuWithName("SleepytimeVillageInventory").TurnOff();
    PlayerMenus.GetMenuWithName ("InGame").TurnOn();

    }


    }


    }

  • Follow up, I've realised that the inventory closing once an item is selected does not help with combining objects, as the player cannot see any other items to combine with, so instead, how do I have it that the inventory bar closes once the cursor is out of range of the inventory bar area? Thanks

  • If your Menu's "Appear type" is set to "Mouse Over", it will turn on/off when the mouse moves over/away from it.

    Othewise, you can read the mouse position directly and turn it off/lock it based on its value:

    private void Update ()
    {
        Vector2 mousePosition = Input.mousePosition;
        if (mousePosition.y < Screen.height * 0.67f)
        {
            // Upper 2/3rds of screen
            PlayerMenus.GetMenuWithName ("MyMenu").isLocked = true;
            PlayerMenus.GetMenuWithName ("MyMenu").TurnOff ();
        }
    }
    
  • edited December 2021

    Have tried this script ands it is not showing my inventory menu at all? see video:

    https://www.dropbox.com/s/obq1m06docxv7ix/MouseOver.mov?dl=0

  • It's a sample script - you will need to adapt it to suit your needs.

  • ah ok, so play with height number?

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.