Hi,
One way to do this is to use DialogueLua.SetVariable() to set 4 variables, one for each answer. In the correct one, call your C# method by
registering it with Lua. Here's an example conversation and some example code:
- NPC Dialogue Text: "What molecule consists of two hydrogen atoms bonded to an oxygen atom?"
- Player Dialogue Text: "[var=Answer1]"
Script: AddScore()
- Player Dialogue Text: "[var=Answer2]"
- Player Dialogue Text: "[var=Answer3]"
- Player Dialogue Text: "[var=Answer14"
Code: Select all
void OnConversationStart(Transform actor)
{
DialogueLua.SetVariable("Answer1", "water");
DialogueLua.SetVariable("Answer2," "nitrogen");
DialogueLua.SetVariable("Answer3," "thermite");
DialogueLua.SetVariable("Answer4," "unobtainium");
}
void OnEnable()
{
Lua.RegisterFunction(nameof(AddScore), this, SymbolExtensions.GetMethodInfo(() => AddScore()));
}
void OnDisable() // (Omit this method if adding the script to the Dialogue Manager.)
{
Lua.UnregisterFunction(nameof(AddScore));
}
void AddScore()
{
score++;
Debug.Log($"Score is now: {score}");
}
If you don't want to use DialogueLua.SetVariable() and [var=
variable], you can register another C# method with Lua that returns a string. For example:
Code: Select all
Lua.RegisterFunction(nameof(GetAnswerText), this, SymbolExtensions.GetMethodInfo(() => GetAnswerText((double)0)));
string GetAnswerText(double i)
{
switch ((int)i)
{
default:
case 1: return "water";
case 2: return "nitrogen";
case 3: return "thermite";
case 4: return "unobtainium";
}
}
Then set up your answer dialogue entries like this:
- Player Dialogue Text: "[lua(GetAnswerText(2))]"
You can also shuffle the order of the answers. See
this post and
this post for examples.
Alternatively, you can create an entire dialogue database and conversation at runtime, described in
this post.