Home > Enterprise >  How to stop the dialogue from returning to the beginning?
How to stop the dialogue from returning to the beginning?

Time:12-13

My dialogue is triggered when the player collides with the object and examines it using the "E" button. However, when the dialogue plays, the player can still press the button, and the dialogue starts from the beginning. How can I disable user input during the dialogue or just stop it from returning to the first sentence? Thank you in advance! This is how the dialogue is triggered:

if (triggered) 
    {
        if (Input.GetKeyDown(KeyCode.E)) {
            onTriggerEnter.Invoke();
}
}

This is the dialogue manager script (I deleted irrelevant lines):

public class DialogueManager : MonoBehaviour {
private Queue<string> sentences;


void Start()
{
    sentences = new Queue<string>();

}
public void StartDialogue(Dialogue dialogue)
{
    sentences.Clear();
   
    foreach (string sentence in dialogue.sentences)
    {
        sentences.Enqueue(sentence);
    }

    DisplayNextSentence();
}

public void DisplayNextSentence()
{
    if (sentences.Count == 0)
    {
        EndDialogue();
        return;
    }
    string sentence = sentences.Dequeue();
    StopAllCoroutines();
    StartCoroutine(TypeSentence(sentence));
}
IEnumerator TypeSentence(string sentence)
{
    dialogueText.text = "";
    foreach (char letter in sentence.ToCharArray())
    {
        dialogueText.text  = letter;
        yield return new WaitForSeconds(typingSpeed);
    }
}
void EndDialogue()
{
  
}

}

CodePudding user response:

I assume you have access to an instance of the DialogueManager object from where you trigger it? In that case you could add a public boolean to the dialogue manager that you set to true in StartDialogue() and false in EndDialogue(). Then just make sure this boolean is false when you press E.

There are likely a number of ways to implement it, but this seems like a simple solution to me.

  • Related