Forum rules - please read before posting.

Examine object when mouse over the Hotspot?

edited February 2020 in Technical Q&A

Hi Chris:)

Is there any way to show some text when mouse over the Hotspot?
The idea is to show some kind of description text to the area when your mouse over. Let say you have a Sword, so when your mouse over it, you see not just a hotspot label but a short description of this Sword somewhere in the corner.

Same for the items in the inventory. Some kind of examine but in your screen. Any ideas?

Comments

  • For the hotspots,
    if you don't mind using custom script you could use OnMouseEnter() and OnMouseExit()

    You could call here an ActionList to avoid futher scripting that would show your menu and set the correct text to it.

  • You'll need some custom scripting - firstly to hook into events that trigger when the Menu turns on, and secondly to actually store the description text.

    Create a new Menu named "Description" and set its Appear type to On Hotspot. Give it a single Label element set to display a Global String Variable value, which we'll update with each descriptive text.

    Then attach this as a component to each Hotspot:

    using UnityEngine;
    using AC;
    
    [RequireComponent (typeof (Hotspot))]
    public class HotspotDescription : MonoBehaviour
    {
    
        public string description;
        private int stringVariableID = 0; // Set this as appropriate
    
        private void OnEnable () { EventManager.OnMenuTurnOn += OnMenuTurnOn; }
        private void OnDisable () { EventManager.OnMenuTurnOn -= OnMenuTurnOn; }
    
        private void OnMenuTurnOn (Menu menu, bool isInstant)
        {
            if (menu.title == "Description") 
            {
                if (KickStarter.playerInteraction.GetActiveHotspot () == GetComponent <Hotspot>())
                {
                    GlobalVariables.SetStringValue (stringVariableID, description);
                }
            }
        }
    
    }
    

    Replace the "0" in the script with the string variable's ID number, and write in the Description text in each Inspector.

    For Inventory items, create a new String Property in the Inventory Manager named Description, and type this in for each item.

    Then attach this to a single GameObject in your scene:

    using UnityEngine;
    using AC;
    
    public class InvItemDescription : MonoBehaviour
    {
    
        public int invPropertyID = 0; // Set this as appropriate
        private int stringVariableID = 0; // Set this as appropriate
    
        private void OnEnable () { EventManager.OnMenuTurnOn += OnMenuTurnOn; }
        private void OnDisable () { EventManager.OnMenuTurnOn -= OnMenuTurnOn; }
    
        private void OnMenuTurnOn (Menu menu, bool isInstant)
        {
            if (menu.title == "Description") 
            {
                if (KickStarter.runtimeInventory.hoverItem != null)
                {
                    string invItemDescription = KickStarter.runtimeInventory.hoverItem.GetProperty (invPropertyID).textVal;
                    GlobalVariables.SetStringValue (stringVariableID, invItemDescription);
                }
            }
        }
    
    }
    

    (Setting the "0" values to match your own)

  • edited May 2020

    Thank you Chris, it works, but I have some additional questions:
    1. Is such description text can be translated then?
    2. Also, I see description text on all hotspots even if I am not adding this script to them. But I want to leave some hotspots without this script to let them work as they typically work.

  • edited May 2020

    :)

  • edited May 2020

    You'd need a different approach, in that case.

    Replace HotspotDescription with the following to support translations:

    using UnityEngine;
    using AC;
    
    public class HotspotDescription : MonoBehaviour, ITranslatable
    {
    
        [SerializeField] private string description;
        [HideInInspector] [SerializeField] private int descriptionID = -1;
    
        public string Description
        {
            get
            {
                return KickStarter.runtimeLanguages.GetTranslation (description, descriptionID, Options.GetLanguage ());
            }
        }
    
        public string GetTranslatableString (int index)
        {
            return description;
        }
    
        public int GetTranslationID (int index)
        {
            return descriptionID;
        }
    
        #if UNITY_EDITOR
    
        public void UpdateTranslatableString (int index, string updatedText)
        {
            description = updatedText;
        }
    
        public int GetNumTranslatables ()
        {
            return 1;
        }
    
        public bool CanTranslate (int index)
        {
            return !string.IsNullOrEmpty (description);
        }
    
        public bool HasExistingTranslation (int index)
        {
            return (descriptionID >= 0);
        }
    
        public void SetTranslationID (int index, int lineID)
        {
            descriptionID = lineID;
        }
    
        public string GetOwner (int index)
        {
            return string.Empty;
        }
    
        public bool OwnerIsPlayer (int index)
        {
            return false;
        }
    
        public AC_TextType GetTranslationType (int index)
        {
            return AC_TextType.Custom;
        }
    
        #endif
    
    }
    

    Then place a single instance of the following in your scene:

    using UnityEngine;
    using AC;
    
    public class SetHotspotDescription : MonoBehaviour
    {
    
        private int stringVariableID = 0; // Set this as appropriate
    
    
        private void OnEnable ()
        {
            EventManager.OnHotspotSelect += OnSelect;
        }
    
        private void OnDisable ()
        {
            EventManager.OnHotspotSelect -= OnSelect;
        }
    
        private void OnSelect (Hotspot hotspot)
        {
            HotspotDescription hotspotDescription = hotspot.GetComponent <HotspotDescription>();
            if (hotspotDescription != null)
            {
                GlobalVariables.SetStringValue (stringVariableID, hotspotDescription.Description);
            }
            else
            {
                GlobalVariables.SetStringValue (stringVariableID, string.Empty);
            }
        }
    
    }
    
  • edited May 2020

    Error:

    Assets/Scripts/HotspotDescription.cs(52,23): error CS0029: Cannot implicitly convert type int' tostring'

  • Hey Chris, buddy, I have some problem with this script. Everything works fine and I see text when I hover mouse on Hotspots, that is really cool. I also can translate it which is also really great! But the problem is that it also shows text when I hover mouse on inventory items. Funny thing is that it display the latest text from the Hotspot where my mouse where on. Please help:)

  • I shared a script above that deals with Inventory items - are you using it?

    How does your inventory appear? Does it turn on at the click of a button / icon, or is it always showing e.g. at the top of the screen?

  • edited July 2020

    Yes, I took both scripts that you gave me and did what you said. I did example for you, so let me please show you step by step. Inventory appears type set to "Mouse Over".

    I have 2 Hotspots with SetHotspotDescription script attached to both. I also have an empty object in the scene with HotspotDescription script attached to this object.

    1. When I first run the game and place mouse over my items, I don't have any description at the bottom, which is correct, cos I already have explore function set to the right mouse button to explore items.
      https://prnt.sc/tieu1v

    (explore items set to the right mouse button)
    https://prnt.sc/tif99i

    1. When I place mouse over the first hotpost, I see description at the bottomTest 1, which is also correct.
      https://prnt.sc/tieuei

    2. But when I go back to my inventory and place mouse over item again, for some reason I start seeing description from the first hotpost Test 1, which should not be there at all.
      https://prnt.sc/tieuk8

    3. If I place mouse over the second hotpost, I see description at the bottomTest 2, which is correct.
      https://prnt.sc/tieur5

    4. And when I go back again to my inventory and place mouse over item again, I start seeing description from the second hotpost Test 2, which should not be there at all too.
      https://prnt.sc/tieuyd

    So, this means that for some reason I start seeing description from hotspots at inventory items but I don't want this, cos I already have explore function set to the right mouse button for items. I want to displace description at the bottom just for hotspots but not for inventory items too. I hope that gives you more explanations.

  • I have 2 Hotspots with SetHotspotDescription script attached to both. I also have an empty object in the scene with HotspotDescription script attached to this object.

    You mean the other way around, no? Each Hotspot should have the HotspotDescription component attached - with only one SetHotspotDescription present in the scene.

    If you want the description to only show when over Hotspots, change its Appear type to During Gameplay and check Start game locked off?. Then, try replacing SetHotspotDescription with this:

    using UnityEngine;
    using AC;
    
    public class SetHotspotDescription : MonoBehaviour
    {
    
        private int stringVariableID = 0; // Set this as appropriate
        private string descriptionMenuName = "DescriptionMenu"; // Set this as appropriate
        private Hotspot hotspotFor;
    
    
        private void OnEnable ()
        {
            EventManager.OnHotspotSelect += OnSelect;
            EventManager.OnHotspotDeselect += OnDeselect;
        }
    
        private void OnDisable ()
        {
            EventManager.OnHotspotSelect -= OnSelect;
            EventManager.OnHotspotDeselect -= OnDeselect;
        }
    
        private void OnSelect (Hotspot hotspot)
        {
            HotspotDescription hotspotDescription = hotspot.GetComponent <HotspotDescription>();
            if (hotspotDescription != null)
            {
                GlobalVariables.SetStringValue (stringVariableID, hotspotDescription.Description);
                PlayerMenus.GetMenuWithName (descriptionMenuName).isLocked = false;
                hotspotFor = hotspot;
            }
        }
    
    
        private void OnDeselect (Hotspot hotspot)
        {
            if (hotspotFor == hotspot)
            {
                PlayerMenus.GetMenuWithName (descriptionMenuName).isLocked = true;
            }
        }
    
    }
    
  • edited July 2020

    Oh, cool! It's working now, thank you Chris! Muuuua
    The only one thing I've changed was the name of the menu, it should be Description but not DescriptionMenu:)

  • edited July 2020

    Hmm, it seems that I have another issue. Now, when I place the mouse over inventory items my menu is not working. I can open menu but option, load, save and other submenus are not working.

  • When did this start occuring, and do they work again if you disable any of the scripts mentioned in this thread?

    Any Console messages?

  • edited July 2020

    There are not any console messages. This start occurring when I changed appear type to During Gameplay. I did a test and set appear type back to On Hotspot and it seems now it's working correct:)

  • Are things working now? I'm not clear on which menu you're referring to.

  • Yes, it's working, thank you. I was talking about Description menu.

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.