[HOWTO] How To: Run Lua Code in Quest Fields

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

[HOWTO] How To: Run Lua Code in Quest Fields

Post by Tony Li »

Some games use custom Lua functions in quests, typically to give a reward when the quest is complete, or to set up an environment when the player accepts a quest. By putting this code in the quest itself, designers can control game activity right in the dialogue database, without having to go into Unity scenes.

This post explains how to set it up.

Let's say you have a C# function named GiveXP() that gives experience points to the player. You've already registered the C# function with Lua (video).

When the player completes a quest, you'd like to call GiveXP(). To do this, create a custom field for the Lua code. In the screenshot below, the quest has a custom field named "Reward Script":

questLua1.png
questLua1.png (17.19 KiB) Viewed 550 times

The last step is to run Reward Script's Lua code when the player completes the quest. Add a script to the Dialogue Manager that has an OnQuestStateChange() method. In this method, check if the quest was completed. If so, run the Reward Script code. Example method:

Code: Select all

void OnQuestStateChange(string quest)
{
    if (QuestLog.IsQuestSuccessful(quest))
    {
        Lua.Run(DialogueLua.GetQuestField(quest, "Reward Script").asString);
    }
}
You can do the same thing for individual quest entries, too, by adding an OnQuestEntryStateChange() method to the script and defining entry-specific custom fields (e.g., "Entry 1 Reward Script", "Entry 2 Reward Script", etc.).
Post Reply