Selecting Dialog Option With Number Keys

Announcements, support questions, and discussion for the Dialogue System.
streethamster
Posts: 13
Joined: Wed Aug 19, 2020 6:14 pm

Re: Selecting Dialog Option With Number Keys

Post by streethamster »

Thank you, Tony. If you have time can you point me in the right direction on this script?

I'm getting confused on the Subtitle parameter, what could I add to make this work? This script is attached to a character.

Update(){

OnConversationLine();
}

public void OnConversationLine(Subtitle subtitle)
{

int danceConversationID = subtitle.dialogueEntry.conversationID;
int danceDialogueID = subtitle.dialogueEntry.id;

if (danceConversationID == 1 && danceDialogueID == 2)
{
anim.Play("Dance");
}
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Selecting Dialog Option With Number Keys

Post by Tony Li »

Hi,

I don't recommend that approach, since it's fragile and depends on internal IDs. But to make it work, make these changes:

Code: Select all

// Remove the Update() and OnConversation() stuff from the top.

public void OnConversationLine(Subtitle subtitle)
{
    int danceConversationID = subtitle.dialogueEntry.conversationID;
    int danceDialogueID = subtitle.dialogueEntry.id;
    if (danceConversationID == 1 && danceDialogueID == 2)
    {
        anim.Play("Dance"); // Assumes anim is already valid.
    }
}
It would be better to define a custom field in your dialogue entries such as "Animation". Then change the method to something like:

Code: Select all

public void OnConversationLine(Subtitle subtitle)
{
    string animatorState = Field.Lookup(subtitle.dialogueEntry.fields, "Animation"); // NOTE: Fixed typo.
    if (!string.IsNullOrEmpty(animatorState))
    {
        anim.Play(animatorState);
    }
}
streethamster
Posts: 13
Joined: Wed Aug 19, 2020 6:14 pm

Re: Selecting Dialog Option With Number Keys

Post by streethamster »

Thank you. I'm getting an error though for "LookupField" and swept the forums for anything similar and came up empty. Most likely I'm not understanding the code.

I go to the dialogue entry, create a custom field called "Animation. The code executes if the Animation field is empty/null, I believe, but how do I have it play a specific animation from the Animator, like "Idle"?
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Selecting Dialog Option With Number Keys

Post by Tony Li »

streethamster wrote: Sun Aug 23, 2020 2:13 amThank you. I'm getting an error though for "LookupField" and swept the forums for anything similar and came up empty. Most likely I'm not understanding the code.
Sorry, that was a typo. I should be "Field.Lookup". I just fixed it in the code example in my previous post.
streethamster wrote: Sun Aug 23, 2020 2:13 amI go to the dialogue entry, create a custom field called "Animation. The code executes if the Animation field is empty/null, I believe, but how do I have it play a specific animation from the Animator, like "Idle"?
In the code above, it will look up the value of the Animation field. If it's not empty/null, it will play it through whatever Animator component is assigned to the variable anim. I was following your code snippets there. If you want to play it through the current speaker's Animator, change it to:

Code: Select all

if (!string.IsNullOrEmpty(animatorState))
{
    Animator anim = subtitle.speakerInfo.transform.GetComponent<Animator>();
    if (anim != null) anim.Play(animatorState);
    else Debug.LogWarning("No Animator found on " + subtitle.speakerInfo.transform + ". Can't play " + animatorState);
}
streethamster
Posts: 13
Joined: Wed Aug 19, 2020 6:14 pm

Re: Selecting Dialog Option With Number Keys

Post by streethamster »

Thank you, and I have a question about dialogue branches.

I know how to link dialogue entries to new conversations, but let's say player picks dialogue entry 2, this is linked to conversation 2, conversation 2 is set to start immediately after. How can I have it set up to be On Trigger or basically the same options that I had with the Dialogue System Trigger is first ran?
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Selecting Dialog Option With Number Keys

Post by Tony Li »

Sorry, I don't understand. What do you mean by On Trigger?
streethamster
Posts: 13
Joined: Wed Aug 19, 2020 6:14 pm

Re: Selecting Dialog Option With Number Keys

Post by streethamster »

Oh sorry, I meant "On Trigger Enter." When you add a Dialogue System Trigger, you have the Trigger drop down menu with On Use, On Start, and other options available. I would like those options when I link conversation 1 to conversation 2, I would like control over when conversation 2 starts or how it starts instead of just continuing right after.
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Selecting Dialog Option With Number Keys

Post by Tony Li »

Hi,

You can use a variable to remember if the player has gone through conversation 1. If the variable is true, then when conversation 1 starts again, you can branch immediately to conversation 2.

For more details and other ideas, see How To: Run a Conversation Only Once. It also contains a section on how to run a different conversation (e.g., conversation 2) after doing conversation 1.
streethamster
Posts: 13
Joined: Wed Aug 19, 2020 6:14 pm

Re: Selecting Dialog Option With Number Keys

Post by streethamster »

Thank you, Tony, I spent two days trying to get a solution, I'm close but I need a few pointers.

I followed the instructions in your link to see if player picks conversation 1, then they should go to conversation 2 when triggered: I have a variable defined "Convo1_Branch1" defined as false. When the player gets to that dialogue entry, the variable is set to true in the script field.

When the player starts a new conversation, I would like to have an if statement in the dialogue entry script field: if(Variable["Convo1_Branch1"==true] then...

How do I start the new conversation? I'm kinda stumped on the exact syntax. Any help would be appreciated!
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Selecting Dialog Option With Number Keys

Post by Tony Li »

There are two ways to handle it:

1. Use two separate Dialogue System Triggers -- one that starts conversation 1, another that starts conversation 2. Each Dialogue System Trigger's Conditions > Lua Conditions should check the value of Variable["Convo1_Branch1"]. This is covered in the link in my previous reply.

2. Or always start the same conversation, and branch based on the value of Variable["Convo1_Branch1"]:

branchVar.png
branchVar.png (17.32 KiB) Viewed 577 times

Note that you don't have to type: Variable["Convo1_Branch1"] == true
You can use the "..." button to select the condition from dropdown menus.
Post Reply