[HOWTO] How To: Quest Timer
Posted: Thu Aug 02, 2018 1:42 pm
Quest Machine has built-in quest timers. The Dialogue System, since it's focused more on dialogue, expects you to handle timers and such yourself. Someone recently asked how to do this, and I thought I'd post the answer here, too.
Let's say you have a quest to disarm a bomb before it blows up. The quest title is "Disarm Bomb", and it has a quest entry "Time left: MM:SS" where MM:SS is the countdown.
To implement this, in your database define a variable (or a custom field in your quest). This example assumes you've defined a variable named "secondsLeft". The quest entry text will look a little crazy, but it shows some of the power of the [lua(code)] tag:
That will format the value of "secondsLeft" as MM:SS. Now you need a way to run the countdown. Use a script similar to this one:
Let's say you have a quest to disarm a bomb before it blows up. The quest title is "Disarm Bomb", and it has a quest entry "Time left: MM:SS" where MM:SS is the countdown.
To implement this, in your database define a variable (or a custom field in your quest). This example assumes you've defined a variable named "secondsLeft". The quest entry text will look a little crazy, but it shows some of the power of the [lua(code)] tag:
Code: Select all
Time left: [lua(string.format("{0:0}:{1:00}", math.floor(Variable["secondsLeft"]/60), Variable["secondsLeft"] % 60))]
Code: Select all
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class TestTimedQuest : MonoBehaviour
{
void Start()
{
InvokeRepeating("SubtractOneSecond", 1, 1);
}
void SubtractOneSecond()
{
var secondsLeft = DialogueLua.GetVariable("secondsLeft").AsInt;
secondsLeft--;
DialogueLua.SetVariable("secondsLeft", secondsLeft);
DialogueManager.SendUpdateTracker(); // secondsLeft changed, so update quest tracker HUD.
if (secondsLeft <= 0)
{
CancelInvoke();
Debug.Log("BOOM!"); // Do something when timer reaches zero.
}
}
}