[HOWTO] How To: Random Quest Goals

Announcements, support questions, and discussion for Quest Machine.
Post Reply
User avatar
Tony Li
Posts: 23272
Joined: Thu Jul 18, 2013 1:27 pm

[HOWTO] How To: Random Quest Goals

Post by Tony Li »

Quest Machine's procedural quest generator is powerful and flexible. For simple randomization, it may be overkill. In these cases, you can instead write simple custom quest actions to assign random values to quest goals.

In the example scene below, the Villager has a new quest called "Random Delivery" that asks the player to deliver a package to the Carrot Field or the Orc Forest:

QM_RandomLocationQuestExample_2025-05-25.unitypackage

Here's how it's set up:

1. The example scene has two new trigger areas: Carrot Field and Orc Forest. When the player enters these trigger areas, a Trigger Event component calls QuestControl.SendToMessageSystem with "Entered" + "Carrot Field" or "Orc Forest".

2. The example adds a new custom quest action named RandomLocationQuestAction. The quest's Waiting To Start > Actions list uses this action to set the value of a tag named "{location}" to either "Carrot Field" or "Orc Forest". (Every quest has a dictionary of tag values that you can add to at runtime.) The custom action script is:

Code: Select all

using UnityEngine;
using System.Collections.Generic;
namespace PixelCrushers.QuestMachine
{
    public class RandomLocationQuestAction : QuestAction
    {
        public List<string> locations;

        public override void Execute()
        {
            base.Execute();
            var randomLocation = locations[Random.Range(0, locations.Count)];
            quest.tagDictionary.dict["{location}"] = randomLocation;
        }
    }
}
3. The quest has one Condition node. It listens for the message "Entered" + "{location}".
Post Reply