Create a conversartion from Game Object variables

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
rauljl1
Posts: 55
Joined: Fri Apr 15, 2022 7:40 pm

Create a conversartion from Game Object variables

Post by rauljl1 »

I hope this is my last question. :lol:
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.
User avatar
Tony Li
Posts: 22149
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create a conversartion from Game Object variables

Post 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.
User avatar
rauljl1
Posts: 55
Joined: Fri Apr 15, 2022 7:40 pm

Re: Create a conversartion from Game Object variables

Post by rauljl1 »

Thank you very much for the reply! :D
User avatar
Tony Li
Posts: 22149
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create a conversartion from Game Object variables

Post 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.
Post Reply