Wait until bool is true using coroutines

Hi,
I am making a dialogue system for a game and want to be able to start a dialogue, wait until the player has read all the sentences and confirmed it before continuing.


This is the coroutine that starts the dialogue:

IEnumerator Story()
{
    //This starts the dialogue
    dialogueTyper.DialogueText(dialogue);

    //I want the coroutine to wait here until the dialogue is done

    //Only then i want to run this...

}

This is the script that handles the actual dialogue typing:

//This is run and starts a coroutine that types the first sentence
public void DialogueText(string[] text)
{
    currentDialogue = text;
    currentSentence = 0;
    StartCoroutine(TypeSentence(currentDialogue[currentSentence]));
}

//This types the sentence letter for letter and shows a button when its done
IEnumerator TypeSentence(string sentence)
{
    string currentSentence = "";
    nextButton.SetActive(false);
    foreach (char letter in sentence)
    {
        currentSentence = currentSentence + letter;
        if (currentSentence == sentence)
        {
            nextButton.SetActive(true);
        }
        textSource.SetText(currentSentence);
        yield return new WaitForSeconds(typingSpeed);
    }
}

//if the user clickes the next button it will start the coroutine again only this time with the next sentence in the array.
//if there is no next sentence it hides the button and sets the text to ""
public void NextSentence()
{
    currentSentence += 1;
    if (currentSentence <= currentDialogue.Length - 1)
    {
        StartCoroutine(TypeSentence(currentDialogue[currentSentence]));
    }
    else
    {
        textSource.SetText("");
        nextButton.SetActive(false);
        //when I reach this line I know that the dialogue is finished.
        //Problem is. How can I comunicate this to the coroutine that started the dialogue in another script?
        //I want to be able to start a dialogue from any script.
    }
}

The only way I can possibly think of is to pass the name of the script that starts the dialogue and store that name in the dialogueTyper script so that the “nextButton” method can find that script by name somehow and change a bool in that script so the coroutine can continue.


Thanks in advance for any answers!

You could do it by passing a callback to your dialogue typer. In your first script it’d look something like this…

IEnumerator Story()
{
   //This starts the dialogue
   dialogueTyper.DialogueText(dialogue, HandleDialogueFinished); 
}

void HandleDialogueFinished()
{
    // Do stuff after dialogue is finished
}

Then in the other script DialogueText would store the callback, and NextSentence would call it when you knew it had finished.

System.Action finishedCallback;
public void DialogueText(string[] text, System.Action c)
{
    finishedCallback = c;
    currentDialogue = text;
    currentSentence = 0;
    StartCoroutine(TypeSentence(currentDialogue[currentSentence]));
} 

public void NextSentence()
{
    currentSentence += 1;
    if (currentSentence <= currentDialogue.Length - 1)
    {
        StartCoroutine(TypeSentence(currentDialogue[currentSentence]));
    }
    else
    {
        textSource.SetText("");
        nextButton.SetActive(false);
        //when I reach this line I know that the dialogue is finished.
        
        finishedCallback();
    }
}