Page 1 of 1

Randomizing Quest Item

Posted: Mon Oct 16, 2023 9:40 am
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!

Re: Randomizing Quest Item

Posted: Mon Oct 16, 2023 12:08 pm
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.

Re: Randomizing Quest Item

Posted: Mon Oct 16, 2023 5:33 pm
by CptStar
Hi Tony thank you for the help, works great.

Re: Randomizing Quest Item

Posted: Mon Oct 16, 2023 8:07 pm
by Tony Li
Glad to help!