Page 1 of 2

Refresh Usable Text

Posted: Wed Nov 15, 2023 7:34 pm
by cptscrimshaw
Hi Tony! Hope you're doing well.

I'd like to update the Name and UseMessage for a Usable through code and have it update live on-screen. I know how to change the text, but it only updates when the player walks away from the game object and goes back to it. Is there a way to refresh the UI without leaving the collider box? I've tried a few things, but none of them seem to be working.

Thanks as always!

Re: Refresh Usable Text

Posted: Wed Nov 15, 2023 7:58 pm
by Tony Li
Hi,

Disable and re-enable the Selector or Proximity Selector. It'll refresh the display.

Re: Refresh Usable Text

Posted: Thu Nov 16, 2023 1:09 am
by cptscrimshaw
I did that and it didn't work, and then I put it in a coroutine to wait for the frame and it didn't work. Am I missing something?

Re: Refresh Usable Text

Posted: Thu Nov 16, 2023 6:58 am
by Tony Li
Hi,

Does the GameObject that has the Selector/ProximitySelector have a SelectorUIStandardUIElements component?

If not, then it's using the default IMGUI method to display the usable text. If this is the case and you don't want to switch to ,SelectorUIStandardUIElements, let me know.

Re: Refresh Usable Text

Posted: Thu Nov 16, 2023 9:24 am
by cptscrimshaw
Morning Tony - yes, the gameobject does have the SelectorUIStandardUIElements component on it.

Re: Refresh Usable Text

Posted: Thu Nov 16, 2023 10:04 am
by Tony Li
Hi,

Here's an example scene:

DS_UpdateUsableTextExample_2023-11-16.unitypackage

It's DemoScene1, but if you press [F1] while targeting Private Hart, it will change his usable UI name.

Re: Refresh Usable Text

Posted: Thu Nov 16, 2023 7:23 pm
by cptscrimshaw
Thanks for the example Tony - that's basically exactly what I tried, however, I'm using a somewhat custom version of ProximitySelector, not Selector. Not sure if that makes a difference. Here is my custom class:

Code: Select all

namespace PixelCrushers.DialogueSystem
{
    [AddComponentMenu("")] // Use wrapper.
    public class ProximitySelectorES : ProximitySelector
    {
        public Usable lastUsable;

        private Animator selectorAnim;

        private void Start()
        {
            selectorAnim = GameObject.Find("Basic Standard UI Selector Elements 1").GetComponent<Animator>();
        }

        void OnDisable()
        {
            if (currentUsable != null)
            {
                lastUsable = currentUsable;
                currentUsable.enabled = false;
                selectorAnim.SetTrigger("Hide");

                //currentUsable.OnDeselectUsable();
            }
        }

        void OnEnable()
        {
            if (lastUsable != null && lastUsable.gameObject.activeInHierarchy)
            {
                //Check NPC indicator status before turning UsableES back on
                if (lastUsable.GetComponent<DialogueActor>() != null)
                {
                    if (lastUsable.GetComponentInChildren<IndicatorManager>().npcIndicatorState != NPCIndicatorState.None)
                    {
                        lastUsable.enabled = true;
                        selectorAnim.SetTrigger("Show");
                    }
                }
                else
                {
                    lastUsable.enabled = true;
                    selectorAnim.SetTrigger("Show");
                }
            }

            if(usablesInRange.Count == 0 && selectorAnim != null)
            {
                selectorAnim.SetTrigger("Hide");
            }
            //if (currentUsable != null) currentUsable.enabled = true;  //OnSelectUsable();
        }

        public void ClearUsable()
        {
            currentUsable = null;
            lastUsable = null;
        }

        /// <summary>
        /// Calls OnUse on the current selection.
        /// </summary>
        public override void UseCurrentSelection()
        {
            //Check priority of Usable and pick the highest number
            int tempUsable = 0;

            foreach (var usable in usablesInRange)
            {
                if (usable.GetComponent<UsableES>() != null)
                {
                    if (usable.GetComponent<UsableES>().priority > tempUsable)
                    {
                        tempUsable = usable.GetComponent<UsableES>().priority;
                        currentUsable = usable.GetComponent<UsableES>();
                    }
                }
            }

            base.UseCurrentSelection();
        }

        protected override void CheckTriggerEnter(GameObject other)
        {
            if (other.GetComponent<UsableES>() != null)
            {
                if (currentUsable == null)
                {
                    base.CheckTriggerEnter(other);
                }
                else
                {
                    if (other.GetComponent<UsableES>().priority > currentUsable.GetComponent<UsableES>().priority)
                    {
                        base.CheckTriggerEnter(other);
                    }
                }
            }
        }

        protected override void OnUsableDisabled(Usable usable)
        {
        }
    }
}

Re: Refresh Usable Text

Posted: Thu Nov 16, 2023 8:13 pm
by Tony Li
Hi,

In your overridden OnEnable() and OnDisable() methods, call the base methods:

Code: Select all

        void OnDisable()
        {
             base.OnDisable();
            if (currentUsable != null) ...
        }

        void OnEnable()
        {
            base.OnEnable();
            if (lastUsable != null && lastUsable.gameObject.activeInHierarchy) ...

Re: Refresh Usable Text

Posted: Fri Nov 17, 2023 9:13 pm
by cptscrimshaw
ProximitySelector.cs doesn't have an OnEnable or OnDisable method. I'm on version 2.2.30.

Am I missing something?

Re: Refresh Usable Text

Posted: Fri Nov 17, 2023 9:31 pm
by Tony Li
I believe it was added in 2.2.33. Can you back up your project and update to the current version?