I hope this is my last question.
I want to create a conversation with 1 question and 4 answers. One of these answers will call a function and the other ones not.
I wish these question and answers are obtained from a script, because that way, every enemy with that script will have a personalized conversation.
I hope you can help me again, Tony.
Create a conversartion from Game Object variables
Re: Create a conversartion from Game Object variables
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:
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:
Then set up your answer dialogue entries like this:
Alternatively, you can create an entire dialogue database and conversation at runtime, described in this post.
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"
- Player Dialogue Text: "[var=Answer1]"
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}");
}
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";
}
}
- Player Dialogue Text: "[lua(GetAnswerText(2))]"
Alternatively, you can create an entire dialogue database and conversation at runtime, described in this post.
Re: Create a conversartion from Game Object variables
Thank you very much for the reply!
Re: Create a conversartion from Game Object variables
Glad to help! I'm finishing work for the night. If you have any questions about implementing this, let me know and I'll answer in the morning.