Randomizing Quest Item

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
CptStar
Posts: 2
Joined: Mon Oct 16, 2023 9:28 am

Randomizing Quest Item

Post by CptStar »

Hi, the game I'm working on has some resources and I need a quest randomly pick one of them and ask a random amount for it. My first attempt is to create an Item field in a quest, named requiredItem, but could not manage to change that field, tried to use DialogueLua.SetQuestField("StarQuest", "RequiredItem", x); for the x, I tried string name of an Item, or index number, none of them worked. Or maybe that approach path is completely wrong?
Also related to this, should I re-create those resources as variables or Items in Dialogue Database to manage this correctly?
Thanks!
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: Randomizing Quest Item

Post by Tony Li »

You don't need to recreate the resources as variables or items.

Here's an example. It increments a variable to keep track of how many items the player has collected. If you use an inventory system and the player can lose items, you'll want to use the inventory system instead of the variable.

Code: Select all

string[] itemNames = new string[] { "apples", "bananas", "cherries" };
string requiredItem = itemNames[Random.Range(0, itemNames.Length)];
DialogueLua.SetQuestField("StarQuest", "RequiredItem", requiredItem);
DialogueLua.SetVariable("StarQuest.ItemsCollected", 0);
Example dialogue text:
  • Dialogue Text: "I want you to find 3 [lua(Quest["StarQuest"].RequiredItem)]."
Example quest content:
  • Display Name: "Collect 3 [lua(Quest["StarQuest"].RequiredItem)]"
  • Entry 1: "[var=StarQuest.ItemsCollected] / 3 Collected"
Example code when the player picks up an item:

Code: Select all

public string myItemName = "apples";

void OnPickUp()
{
    if (myItemName == DialogueLua.GetQuestField("StarQuest", "RequiredItem").asString)
    {
        DialogueLua.SetVariable("StarQuest.ItemsCollected", DialogueLua.GetVariable("StarQuest.ItemsCollected") + 1);
    }
}
Side note: If you need to do a lot of this, you might be interested in Quest Machine. It natively handles procedurally-generated quests.
CptStar
Posts: 2
Joined: Mon Oct 16, 2023 9:28 am

Re: Randomizing Quest Item

Post by CptStar »

Hi Tony thank you for the help, works great.
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: Randomizing Quest Item

Post by Tony Li »

Glad to help!
Post Reply