Can I run lua script when a quest begins?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
effalumper
Posts: 4
Joined: Wed Dec 11, 2024 9:09 am

Can I run lua script when a quest begins?

Post 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?
User avatar
Tony Li
Posts: 23259
Joined: Thu Jul 18, 2013 1:27 pm

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

Post 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 615 times

Quest:

questStartCode.png
questStartCode.png (39.51 KiB) Viewed 615 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.)
effalumper
Posts: 4
Joined: Wed Dec 11, 2024 9:09 am

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

Post 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
User avatar
Tony Li
Posts: 23259
Joined: Thu Jul 18, 2013 1:27 pm

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

Post by Tony Li »

Happy to help!
Post Reply