Best way for dialogue and choices?

Hello,

I’m newish to Unity and wanting to make a Visual Novel based game that has a dialogue system which has a choice system that also remembers certain choices as well as doing seperate dialogue for each choice.

However the way I have done it is not the best, I have the code I have below:

public string[] names;
public string[] sentences;
public Sprite[] characters;

public TextMeshProUGUI nameDisplay;
public TextMeshProUGUI dialogueDisplay;
public Image characterBox;
public GameObject continueButton;
public AudioSource audioSource;

public float typingSpeed;

private int index;

void Start()
{
    audioSource.Play();
    StartCoroutine(Type());
    nameDisplay.text = names[index];
    characterBox.sprite = characters[index];
}

void Update()
{
    if(dialogueDisplay.text == sentences[index])
    {
        continueButton.SetActive(true);
        audioSource.Stop();
    }

    if (Input.GetMouseButton(0))
    {
        typingSpeed = 0.005f;
    } else
    {
        typingSpeed = 0.02f;
    }
}

IEnumerator Type()
{
    foreach(char letter in sentences[index].ToCharArray())
    {
        dialogueDisplay.text += letter;
        yield return new WaitForSeconds(typingSpeed);
    }
}

public void NextSentence()
{
    audioSource.Play();
    continueButton.SetActive(false);

    if(index == sentences.Length - 1)
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

    if(index < sentences.Length - 1)
    {
        index++;

        dialogueDisplay.text = "";
        nameDisplay.text = "";

        nameDisplay.text = names[index];
        characterBox.sprite = characters[index];

        StartCoroutine(Type());
    } else
    {
        dialogueDisplay.text = "";
        nameDisplay.text = "";

        nameDisplay.text = names[index];
        characterBox.sprite = characters[index];

        continueButton.SetActive(false);
    }
}

How would I start on a system that would allow for choices that trigger and get remembered, as well as choices leading to different dialogue paths. What’s the best route for this?

To remember previous choices: what I do is have some variables that are stored, such as integers or booleans. When the character makes a choice that will be affected later, change the value of that variable. Then check the value to see what dialogue should be triggered.

Perhaps harder to think through is how to arrange the dialogue. I store it all in functions with appropriate names, like Introduction() or WhenAgreeToQuest(), that call other functions when a given choice is selected.

An example would probably help most, though you would obviously need to adapt it. Let’s say that at some point one of the characters asks for assistance with a problem. You could have 2 buttons on the screen (or something other than a button, depending on how you want the player to be able to choose, but you can easily adapt this) saying “I’ll help” (on button1) or “Maybe later” (on button2). If you are using buttons for the player’s input, you could write something like button1.onClick.AddListener(() => {remove listeners and stuff here… WhenAgreeToQuest() }) which would trigger when button1 is pressed (meaning, in this case, that the player wants to do the quest). WhenAgreeToQuest would be where you play all of the audio and proceed with the story, and perhaps set a variable signifying the player’s choice for that. You would also add more buttons with more, new choices that call more functions. This will allow your dialogue to branch well and proceed in an orderly fashion. Add a few if-statements to adjust dialogue based on previous choices and you’re all set.

I’m not sure if that’s the best way to do it, but it’s been working quite well for me.