Page 1 of 1

Can I run lua script when a quest begins?

Posted: Wed Dec 11, 2024 9:17 am
by effalumper
I'd like to run some lua script when a quest begins. I think I can probably do this by attaching script to the dialog manager that has a OnQuestStateChange method... but I was wondering if there is a way to just specify Lua script to run in the dialog database somehow?

Re: Can I run lua script when a quest begins?

Posted: Wed Dec 11, 2024 2:21 pm
by Tony Li
Hi,

Yes -- you can add a custom Text field to your quest template. Put the Lua code in there.

Then add a script with an OnQuestStateChange method to the Dialogue Manager. When a quest state changes to Active, check if the quest has the Lua code field. If so, run the Lua code.

For example, template:
questTemplateStartCode.png
questTemplateStartCode.png (67.64 KiB) Viewed 623 times

Quest:

questStartCode.png
questStartCode.png (39.51 KiB) Viewed 623 times

Code:

Code: Select all

void OnQuestStateChange(string questName)
{
    if (QuestLog.IsQuestActive(questName))
    {
        var startCode = DialogueLua.GetQuestField(questName, "StartCode");
        Lua.Run(startCode);
    }
}
You can also do the same thing to give quest rewards. For example, add a custom field named RewardCode. When the quest goes into the Success state, look up this field and run it. You could have it call C# methods to give XP, give items, etc. (Register your C# methods with Lua.)

Re: Can I run lua script when a quest begins?

Posted: Thu Dec 12, 2024 10:28 am
by effalumper
Hi again,

Ah, nice - I hadn't thought of using a text field and running the code myself. Thanks for the quick response! Kind regards,

Andy

Re: Can I run lua script when a quest begins?

Posted: Thu Dec 12, 2024 10:36 am
by Tony Li
Happy to help!