Page 1 of 1
Create a conversartion from Game Object variables
Posted: Fri Apr 15, 2022 8:53 pm
by rauljl1
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.
Re: Create a conversartion from Game Object variables
Posted: Fri Apr 15, 2022 9:40 pm
by Tony Li
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.
Re: Create a conversartion from Game Object variables
Posted: Fri Apr 15, 2022 9:52 pm
by rauljl1
Thank you very much for the reply!
Re: Create a conversartion from Game Object variables
Posted: Fri Apr 15, 2022 9:59 pm
by Tony Li
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.