[HOWTO] How To: Run Lua Code in Quest Fields
Posted: Thu Aug 06, 2020 9:42 am
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":
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:
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.).
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":
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);
}
}