Interactable Message Improvement

Woods

New member
I was wanting responsive messages for my interactables and found it odd that IInteractableTarget functions use the character gameobject but the IInteractableMessage doesn't seeing as these are used together. So I made a few minor changes to make it work. This is easily added and won't mess with your existing AbilityMessage functions as its optional to use.

Update IInteractableMessage.cs with the optional function that takes GameObject parameter
C#:
    public interface IInteractableMessage
    {
        /// <summary>
        /// Returns the message that should be displayed when the object can be interacted with.
        /// </summary>
        /// <returns>The message that should be displayed when the object can be interacted with.</returns>
        string AbilityMessage();

        //Optional interface, uses default if not implemented
        string AbilityMessage(GameObject character)
        {
            return AbilityMessage();
        }
    }

Update Interactable.cs AbilityMessage to take a GameObject parameter and use it to call the new optional interface function
C#:
        public string AbilityMessage(GameObject character)
        {
            if (m_InteractableTargets != null) {
                for (int i = 0; i < m_InteractableTargets.Length; ++i) {
                    // Returns the message from the first IInteractableMessage object.
                    if (m_InteractableTargets[i] is IInteractableMessage) {
                        return (m_InteractableTargets[i] as IInteractableMessage).AbilityMessage(character);
                    }
                }
            }
            return string.Empty;
        }

Update the Interact.cs AbilityMessageText to pass the character GameObject to the updated Interactable function
C#:
        public override string AbilityMessageText
        {
            get
            {
                var message = m_AbilityMessageText;
                if (m_Interactable != null) {
                    message = string.Format(message, m_Interactable.AbilityMessage(m_GameObject));
                }
                return message;
            }
            set { base.AbilityMessageText = value; }
        }

Once its setup you can implement the AbilityMessage(GameObject character) in any of your IInteractableMessages and do some logic using the character to show conditional messages. If its implemented, it takes precedence over the required AbilityMessage(), which you can leave empty.

You probably don't want to do anything too complex in this new function as I think it gets called repeatedly while you are detecting the interactable. It seems fine with how I have used it to do a couple TryGetComponents and checking some values on the inventory and active character item.
 
Last edited:
Top