Page 1 of 1

[HOWTO] How To: Show Counter Remaining

Posted: Mon Mar 01, 2021 10:05 pm
by Tony Li
Quest Machine handles inline tags of the format {#counter}, {>#counter}, etc., so you can include tags like this in your quests' text content:

{#carrots}/{>#carrots} Carrots Picked

It does not have a tag to show the amount remaining -- that is the counter's max value minus the current value. However, you can add a custom quest content script to do that. Here are some scripts:
CounterRemainingQuestContent.cs

Code: Select all

using UnityEngine;

namespace PixelCrushers.QuestMachine
{

    /// <summary>
    /// Shows the counter amount remaining: (max - current)
    /// </summary>
    public class CounterRemainingQuestContent : BodyTextQuestContent
    {

        [SerializeField]
        private StringField m_label = new StringField();

        [Tooltip("Index of a counter defined in the quest. Inspect the quest's main info to view/edit counters.")]
        [SerializeField]
        private int m_counterIndex = 0;

        public override string GetEditorName()
        {
            var counter = (quest != null) ? quest.GetCounter(m_counterIndex) : null;
            var counterName = (counter != null && !StringField.IsNullOrEmpty(counter.name)) ? counter.name.value : "Counter";
            return (StringField.IsNullOrEmpty(m_label) ? "Remaining: " : m_label.value) + counterName;
        }

        public override string runtimeText
        {
            get
            {
                var counter = (quest != null) ? quest.GetCounter(m_counterIndex) : null;
                return (counter != null)
                    ? (StringField.GetStringValue(m_label) + (counter.maxValue - counter.currentValue))
                    : StringField.GetStringValue(m_label);
            }
        }
    }
}
CounterRemainingQuestContentEditor.cs (put in a folder named Editor)

Code: Select all

using UnityEngine;
using UnityEditor;

namespace PixelCrushers.QuestMachine
{

    [CustomEditor(typeof(CounterRemainingQuestContent), true)]
    public class CounterRemainingQuestContentEditor : QuestSubassetEditor
    {

        private string[] m_nameList = null;

        protected override void OnEnable()
        {
            base.OnEnable();
            if (target == null || serializedObject == null) return;
            m_nameList = QuestEditorUtility.GetCounterNames();
        }

        protected override void Draw()
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty("m_label"));
            QuestEditorUtility.EditorGUILayoutCounterNamePopup(serializedObject.FindProperty("m_counterIndex"), m_nameList);
            if (GUILayout.Button("Refresh Counter Names")) m_nameList = QuestEditorUtility.GetCounterNames();
        }

    }
}

Re: [HOWTO] How To: Show Counter Remaining

Posted: Tue Mar 02, 2021 8:22 am
by Tony Li
Here's an alternate form that lets you wrap your text in rich text codes or TextMesh Pro tags. Use {0} where you want the counter remaining value to appear, as in: "<color=red>Carrots Left: {0}</color>"
CounterRemainingQuestContent.cs

Code: Select all

using UnityEngine;

namespace PixelCrushers.QuestMachine
{

    /// <summary>
    /// Shows the counter amount remaining: (max - current)
    /// </summary>
    public class CounterRemainingQuestContent : BodyTextQuestContent
    {

        [Tooltip("Include {0} where you want counter remaining value to appear.")]
        [SerializeField]
        private StringField m_label = new StringField();

        [Tooltip("Index of a counter defined in the quest. Inspect the quest's main info to view/edit counters.")]
        [SerializeField]
        private int m_counterIndex = 0;

        public override string GetEditorName()
        {
            var counter = (quest != null) ? quest.GetCounter(m_counterIndex) : null;
            var counterName = (counter != null && !StringField.IsNullOrEmpty(counter.name)) ? counter.name.value : "Counter";
            return (StringField.IsNullOrEmpty(m_label) ? "Remaining: " : m_label.value) + counterName;
        }

        public override string runtimeText
        {
            get
            {
                var counter = (quest != null) ? quest.GetCounter(m_counterIndex) : null;
                return (counter != null)
                    ? (string.Format(StringField.GetStringValue(m_label), (counter.maxValue - counter.currentValue)))
                    : StringField.GetStringValue(m_label);
            }
        }
    }
}