Generic UI Message

RobotGames

Member
I do not see a way I can send a generic message to the UI without basically adding to the message monitor. I'd hate that because then if I update all my changes have to be transferred over. Right now I see that it only displays messages from Abilities and Object Pickups.
 
As you mentioned the Message Monitor only listens for ability and object input. Moving this one to the feature requests board - how would you expect to call the Message Monitor in order to show any arbitrary text?

Also, which has priority if an ability or object message is later set when an arbitary message is already shown?
 
I was looking at making additions myself, I'd literally pass a string to be displayed? I was going to use the Event system something along the lines of:

EventHandler.RegisterEvent<string>(m_Character, "OnDisplayString", OnDisplayString);

Where OnDisplayString would deal with of course displaying the string much like OnObjectPickup however with the string passed in. As for the priority I'd make it the lowest. Just thinking about what I'd want to know as a player. Personally I was planning on using the messages to display info on things that do not fit into the structures we have now. Like I have a couple variable health pickups, on the one using standard pickup I set the PickupMessageText before I call ObjectPickedUp, but Interactable only has an interact message, not a message for completed or such. Or what about something like picking a lock, press <ACTION> to pick lock... then what about fail? press <ACTION> failed to pick lock? It would be nice to have the ability to use the existing system without going outside.

While you're at it how about support for TextMeshPro? I went through the text I use and changed them myself. I wonder if you really have to suppport both anymore as TMP is a default Unity Package.
 
Last edited:
Thanks for the notes. I have it on my list.

While you're at it how about support for TextMeshPro? I went through the text I use and changed them myself. I wonder if you really have to suppport both anymore as TMP is a default Unity Package.
The controller supports 2017.4 and this was before the package system really started to be used by Unity. Eventually Unity will be distributing everything through the package manager and this will make it easier to have dependencies such as TextMesh Pro.
 
This was something I needed as well. FWIW, here's how I modified the Message Monitor to handle generic message requests. My scheme prioritizes the generic messages and is probably a little bit kludgey, but it works for my needs:

C#:
/// ---------------------------------------------

   // ...

   // after the existing private globals, add vars for the string & sprite

        private string m_String;
        private Sprite m_Sprite;

        // unregister/register the new event handler

        /// <summary>
        /// Attaches the monitor to the specified character.
        /// </summary>
        /// <param name="character">The character to attach the monitor to.</param>
        protected override void OnAttachCharacter(GameObject character)
        {
            if (m_Character != null)
            {
                EventHandler.UnregisterEvent<Ability, bool>(m_Character, "OnAbilityMessageCanStart", OnAbilityCanStart);
                EventHandler.UnregisterEvent<ObjectPickup>(m_Character, "OnObjectPickedUp", OnObjectPickedUp);
                EventHandler.UnregisterEvent<string, Sprite>(m_Character, "OnDisplayMessage", OnDisplayMessage);
            }

            base.OnAttachCharacter(character);

            if (m_Character == null)
            {
                return;
            }

            EventHandler.RegisterEvent<Ability, bool>(m_Character, "OnAbilityMessageCanStart", OnAbilityCanStart);
            EventHandler.RegisterEvent<ObjectPickup>(m_Character, "OnObjectPickedUp", OnObjectPickedUp);
            EventHandler.RegisterEvent<string, Sprite>(m_Character, "OnDisplayMessage", OnDisplayMessage);
        }


// the new arbitrary message handler

        /// <summary>
        /// Display an arbitrary message.
        /// </summary>
        /// <param name="displayString">String to display</param>
        /// <param name="sprite">Icon to display</param>
        private void OnDisplayMessage(string displayString, Sprite sprite)
        {
            m_String = displayString;
            m_Sprite = sprite;
            m_ShouldFade = true;
            m_ObjectAlphaColor = 1;
            if (m_ShouldFade)
            {
                Scheduler.Cancel(m_ScheduledFade);
            }
            UpdateMessage();
        }

// original handlers go here; omitted for clarity


// modified UpdateMessage function

        /// <summary>
        /// Updates the text and icon UI.
        /// </summary>
        private void UpdateMessage()
        {
           
            if (m_Text != null)
            {
                if (m_String != null) // use the arbitrary string if it exists
                {
                    m_Text.text = m_String;
                }
                else // use the original logic (abilities have priority)
                {
                    m_Text.text = m_Ability != null ? m_Ability.AbilityMessageText : (m_ObjectPickup != null ? m_ObjectPickup.PickupMessageText : string.Empty);

                }
                m_Text.enabled = !string.IsNullOrEmpty(m_Text.text);
            }
            if (m_Icon != null)
            {
                if (m_Sprite != null) // use the abitrary icon if it exists
                {
                    m_Icon.sprite = m_Sprite;
                }
                else
                {
                    m_Icon.sprite = m_Ability != null ? m_Ability.AbilityMessageIcon : (m_ObjectPickup != null ? m_ObjectPickup.PickupMessageIcon : null);

                }
                m_Icon.enabled = m_Icon.sprite != null;
            }

            // The message will fade if an object is picked up.
            var messageVisible = m_Ability != null || m_ObjectPickup != null || m_String != null || m_Sprite != null; // check for arbitrary items as well
            if (messageVisible)
            {
                if (m_Text != null)
                {
                    var color = m_Text.color;
                    color.a = 1;
                    m_Text.color = color;
                }
                if (m_Icon != null)
                {
                    var color = m_Icon.color;
                    color.a = 1;
                    m_Icon.color = color;
                }

                if (m_ShouldFade)
                {
                    m_ScheduledFade = Scheduler.Schedule(m_ObjectVisiblityDuration, FadeMessage);
                }
                m_String = null; // reset the abitrary string & sprite
                m_Sprite = null;
            }

            m_GameObject.SetActive(m_ShowUI && (messageVisible || m_ShouldFade));
        }

      // ... rest of code
}
 
Top