Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How can I do that with Queue<string>?

Discussion in 'Scripting' started by Edugonza, Jul 1, 2018.

  1. Edugonza

    Edugonza

    Joined:
    Apr 19, 2018
    Posts:
    8
    Hi!

    I want to make a dialog and i'm having some problems. The sentences come with a animation, the words are being inserted 1 by 1. I want to make that when I click on the Continue button and there is a animation of a sentence i want to make the system end the sentence instead of going for the next one without showing the full sentence. I have problems to do that because I dont know how to make a definition of the string value sentence and show it with the current sentece of my Queue string. Is it possible?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class DialogueManager : MonoBehaviour {
    8.  
    9.     public Text nameText;
    10.     public Text dialogueText;
    11.     public Queue<string> sentences;
    12.     public bool canClick = true;
    13.  
    14.    
    15.  
    16.  
    17.     public Animator animator;
    18.     // Use this for initialization
    19.     void Start () {
    20.  
    21.         sentences = new Queue<string> ();
    22.        
    23.     }
    24.    
    25.     public void StartDialogue (Dialogue dialogue){
    26.  
    27.  
    28.         animator.SetBool ("IsOpen", true);
    29.         nameText.text = dialogue.name;
    30.  
    31.         sentences.Clear ();
    32.  
    33.         foreach (string sentence in dialogue.sentences) {
    34.  
    35.             sentences.Enqueue (sentence);
    36.         }
    37.  
    38.        
    39.        
    40.         DisplayNextSentence ();
    41.     }
    42.  
    43. //click with the continue button
    44.     public void DisplayNextSentence()
    45.     {
    46.  
    47.         if (canClick == false)
    48.         {
    49.             StopAllCoroutines();
    50.          
    51.             /* string sentence = ??;  the current value of the queue so i can show the text that is currently showing with animation */
    52.  
    53.  
    54.             canClick = true;
    55.  
    56.         }
    57.  
    58.        else  if (canClick == true) {
    59.             canClick = false;
    60.  
    61.        
    62.        
    63.  
    64.        
    65.  
    66.  
    67.         if (sentences.Count == 0)
    68.         {
    69.  
    70.             EndDialogue();
    71.             return;
    72.         }
    73.  
    74.         string sentence = sentences.Dequeue();
    75.      
    76.        
    77.      
    78.      
    79.        
    80.  
    81.         StartCoroutine(TypeSentence(sentence));
    82.  
    83.  
    84.      
    85.  
    86.  
    87.     }
    88.  
    89.     }
    90.  
    91.     IEnumerator TypeSentence (string sentence){
    92.  
    93.         dialogueText.text = "";
    94.         foreach (char letter in sentence.ToCharArray()) {
    95.  
    96.             dialogueText.text += letter;
    97.            
    98.             yield return null;
    99.         }
    100.         canClick = true;
    101.  
    102.        
    103.     }
    104.  
    105.     void EndDialogue(){
    106.  
    107.         Debug.Log ("End Of conversation");
    108.         animator.SetBool ("IsOpen", false);
    109.     }
    110. }
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Are you saying that, if
    TypeSentence
    is currently executing and the continue button is clicked, you want to terminate the
    TypeSentence
    function immediately?

    If so, you can add a public method (called say
    StopWritingSentence
    ). That method would stop the internal Coroutine from running.

    You would need to hook your button click code through to
    StopWritingSentence
    . The Coroutine can be stopped either directly by a handle/ string or using StopAllCoroutines (which you are already using).

    Or are you wanting something other than that?
     
  3. Edugonza

    Edugonza

    Joined:
    Apr 19, 2018
    Posts:
    8
    Yes, I am. I stop the Coroutine but the problem is that the character set is stopped with it. So I want to print the entire sentence but I dont know how to take it as a string to show it.
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I see. You could maybe try something like this:
    Code (CSharp):
    1. IEnumerator TypeSentence(string sentence)
    2. {
    3.     dialogueText.text = "";
    4.     m_showWholeSentence = false;
    5.  
    6.     foreach (char letter in sentence.ToCharArray())
    7.     {
    8.         dialogueText.text += letter;
    9.  
    10.         if(!m_showWholeSentence)
    11.             yield return null;
    12.     }
    13.     canClick = true;
    14. }
    15.  
    16. bool m_showWholeSentence;
    Now, when the click is detected, you no longer need to stop the Coroutine, simply set
    m_showWholeSentence
    to true. The Coroutine will rapidly print out the remainder of the sentence (without yielding) and will terminate naturally.
     
  5. Edugonza

    Edugonza

    Joined:
    Apr 19, 2018
    Posts:
    8
    Wow. Really thank you. I was having a headache with that thing.
     
    Doug_B likes this.
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Glad it helped out. :)