Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

AutoType text, Coroutines, and how to not to use them.

Discussion in 'Scripting' started by Micio_del_Cheshire, Sep 14, 2018.

  1. Micio_del_Cheshire

    Micio_del_Cheshire

    Joined:
    Oct 24, 2013
    Posts:
    14
    Hello there.
    I was trying to get a simple text to autotype its content (typewriter style).
    I'm parsing the text content, some of it has to remain written, some of it has to be removed after being written.

    This is the code about autotyping.

    Code (CSharp):
    1.  public void AutoType(string s)
    2.     {
    3.         if (!typing)
    4.         {
    5.             stringToPrint = s;
    6.  
    7.             StartCoroutine(Type(s));
    8.         }
    9.         else
    10.         {
    11.             Debug.LogError("is typing");
    12.         }
    13.     }
    14.  
    15.     private IEnumerator Type(string s)
    16.     {
    17.        typing = true;
    18.        foreach (char c in s.ToCharArray())
    19.        {
    20.             text.text += c;
    21.             yield return new WaitForSeconds(typeSpeed);
    22.        }
    23.         Debug.Log("FINISHED");
    24.        typing = false;
    25.     }
    The question is: I'm using Coroutines, ad I don't want to. Coroutines allow the program flow to continue. In this way I'm writing a temporary text context, and in the meantime it's been autotyped, the game is already parsing and processing the next text, messing with the removal process afterwards.

    I'm ok with the game to wait, but I can't make the game wait unless using coroutines.
    So:
    - how can I autotype a text, being able to know when it finishes typing? I tried something like
    Code (CSharp):
    1. autotext.AutoType(string);
    2. while(autotext.IsTyping);
    but it's going in endless loop.

    Thanks for the help
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    1,682
    So the issue is that you have more than one phrase to typewriter text and the program is getting ahead of itself? You can have one coroutine wait for the other coroutine to finish before continuing to overcome that:

    Code (csharp):
    1.  
    2. IEnumerator BeginSpeech(List<string> phrases)
    3. {
    4.    for(int i = 0; i < phrases.Count; ++i)
    5.    {
    6.        yield return StartCoroutine(Type(phrases[i]));
    7.    }
    8. }
    9.  
    That will wait for Type to finish before moving on to the next phrase.
     
  3. Micio_del_Cheshire

    Micio_del_Cheshire

    Joined:
    Oct 24, 2013
    Posts:
    14
    Thanks, I will try it :)
     
  4. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    2,856
    You dont need todo StartCoroutine

    Code (CSharp):
    1. private IEnumerator BeginSpeech(List<string> phrases)
    2. {
    3.    foreach(var phrase in phrases)
    4.       yield return Type(phrase);
    5. }