Page 1 of 1

Timer in Quest

Posted: Tue Apr 24, 2018 3:56 pm
by Leonardo
Hello Im looking to activate a timer to do my quest, so my question guys is... Timers countdown is integrated in the system or I should Implement it, I search in the documentation but didn't found anything I'm sorry if I didn't search very well and the info is there I tough it was because yesterday looking http://www.pixelcrushers.com I saw a video of Quest Machine and the timer was there, but maybe is just in Quest machine hehe that's why Im asking, Btw Quest Machine Looks great is just that right now I just need the timer so Maybe if it's no available in DSFU I should implement it and use message for communicate the mission is failed if the time is over right :P? I'm just a hobbyist hehe so I'm a newbie and Im always confused hehe Thanks!!

Re: Timer in Quest

Posted: Tue Apr 24, 2018 10:22 pm
by Tony Li
Hi,

Timers are built into Quest Machine, but you'll have to implement them yourself for the Dialogue System.

It'll be easier if you're comfortable with scripting or using a visual scripting tool like Playmaker. Or you can just use the script in this example scene: TimedQuestExample_2018-04-24.unitypackage

In the example quest, you have 20 seconds to pick up 3 blocks. To set it up, I added a QuestTimer script (included in the package) and configured it to track the time left in a dialogue database variable named BlockTimeLeft. If the time runs out, it calls a Quest Trigger that sets the quest to failure.

Each block has an Increment On Destroy component that increments another variable named NumBlocks and then calls another Quest Trigger that checks if NumBlocks is at least 3. If so, it sets the quest to success.

The QuestTimer script is here, too, for reference:

QuestTimer.cs

Code: Select all

using UnityEngine;
using UnityEngine.Events;
using PixelCrushers.DialogueSystem;

public class QuestTimer : MonoBehaviour
{
    public string timerVariable;
    public float maxTime;
    public bool startTimerImmediately;
    public UnityEvent onTimerStart = new UnityEvent();
    public UnityEvent onTimerEnd = new UnityEvent();

    private float timeLeft = 0;

    private void Start()
    {
        // Check if we need to start the timer as soon as the scene starts:
        if (startTimerImmediately) StartTimer();
    }

    public void StartTimer()
    {
        // To start the timer, set timeLeft to maxTime and invoke the notification events:
        timeLeft = maxTime;
        onTimerStart.Invoke();
        DialogueManager.SendUpdateTracker();
    }

    private void Update()
    {
        // Every frame, check timeLeft:
        if (timeLeft > 0)
        {
            var prevTimeLeft = timeLeft;
            timeLeft -= Time.deltaTime;
            var timeLeftInt = Mathf.FloorToInt(timeLeft);
            // If we crossed a whole second boundary, update variable:
            if (timeLeftInt != Mathf.FloorToInt(prevTimeLeft))
            {
                DialogueLua.SetVariable(timerVariable, Mathf.FloorToInt(timeLeft));
                DialogueManager.SendUpdateTracker();
            }
            if (timeLeftInt == 0) onTimerEnd.Invoke();
        }
    }


    // Hook into save system so timer gets saved:

    private void OnEnable()
    {
        PersistentDataManager.RegisterPersistentData(gameObject);
    }

    private void OnDisable()
    {
        PersistentDataManager.UnregisterPersistentData(gameObject);
    }

    public void OnApplyPersistentData()
    {
        timeLeft = DialogueLua.GetVariable(timerVariable).AsFloat;
    }
}

Re: Timer in Quest

Posted: Wed Apr 25, 2018 12:33 pm
by Leonardo
Thanks you so much!! I will check it to learn how it works according to you explanation, (So I can implement it in my game) once again thanks for the fast response :)

Re: Timer in Quest

Posted: Wed Apr 25, 2018 2:54 pm
by Tony Li
Glad to help!