- Home /
How can I make a function wait before it starts running again?
Hi! I need help with my codes. I'm trying to make a dialogue for my game and it's working fine. When I press the A button on the keyboard, the dialogue will continue on being displayed until the end of the line (kind of like the ones in Pokemon). The problem is that when I press the A button really fast, it won't stop before heading on to the next lines of the dialogue. I've already tried yield but it won't work. Here are my codes:
 #pragma strict
 
 import System;
 import System.IO;
 import System.Text;
 
 var skin : GUISkin;
 private var path : String = "C:/Users/quevin/Documents/Goopi Club/Goopi Club/Assets/Text Files/New Game.txt";
 private var strFile : String;
 private var lines : String [];
 private var txtDialogue : String = "";
 private var delay : float = 0.005;
 private var linePos : int = 0;
 private var i : int;
 private var j : int;
 private var checkPunct = false;
 
 function Start()
 {
     var reader : StreamReader = new File.OpenText(path);
     strFile = reader.ReadToEnd();
     reader.Close();
     
     lines = strFile.Split("\n"[0]);    
     TextDialogue2();
 }
 
 function Update()
 {
     if(Input.GetKeyDown(KeyCode.A))
     {
         if(linePos != lines.Length) TextDialogue2();
         else print("All lines have been printed.");
     }
 }
 
 function OnGUI()
 {
     GUI.skin = skin;
     GUI.BeginGroup(Rect(20, 10, 700, 580));
         
         GUI.BeginGroup(Rect(0, 455, 700, 125));
             
             GUI.Box(Rect(0, 0, 700, 125), "");
             GUI.Label(Rect(120, 35, 685, 110), txtDialogue);
 
         GUI.EndGroup();
     
     GUI.EndGroup();
     
 
 }
 
 function TextDialogue()
 {
     txtDialogue = "";
     checkPunct = false;
     for(i = linePos; i < (linePos + 2); i++)
     {
         for(j = 0; j < lines[i].Length; j++)
         {
             txtDialogue = txtDialogue + lines[i].Substring(j, 1);
             yield WaitForSeconds(delay);
             if(lines[i].Substring(lines[i].Length - 2, 1).Equals(".") || lines[i].Substring(lines[i].Length - 2, 1).Equals("!") || lines[i].Substring(lines[i].Length - 2, 1).Equals("?")) checkPunct = true;
         }
         
         if(checkPunct == true)
         {
             i = i + 1;
             break;
         }
         
         txtDialogue = txtDialogue + "\n";
     }
     
     linePos = i;
 }
 
 function TextDialogue2()
 {
     yield TextDialogue();
 }
Answer by clunk47 · Jul 24, 2013 at 03:04 AM
Just use a boolean that allows / disallows interaction based on if the line is finished typing. Tested this and works like a charm on my end.
 #pragma strict
 
 import System;
 import System.IO;
 import System.Text;
 
 var skin : GUISkin;
 private var path : String = "C:/Users/quevin/Documents/Goopi Club/Goopi Club/Assets/Text Files/New Game.txt";
 private var strFile : String;
 private var lines : String [];
 private var txtDialogue : String = "";
 private var delay : float = 0.005;
 private var linePos : int = 0;
 private var i : int;
 private var j : int;
 private var checkPunct = false;
 private var autotype : boolean = false;
 
 function Start()
 {
     var reader : StreamReader = new File.OpenText(path);
     strFile = reader.ReadToEnd();
     reader.Close();
     
     lines = strFile.Split("\n"[0]);    
     TextDialogue2();
 }
 
 function Update()
 {
     if(Input.GetKeyDown(KeyCode.A))
     {
         //Only allow interaction when autotype is true.
         if(autotype)
         {
             if(linePos != lines.Length) TextDialogue2();
             else print("All lines have been printed.");
         }
     }
 }
 
 function OnGUI()
 {
     GUI.skin = skin;
     GUI.BeginGroup(Rect(20, 10, 700, 580));
     GUI.BeginGroup(Rect(0, 455, 700, 125));
     GUI.Box(Rect(0, 0, 700, 125), "");
     GUI.Label(Rect(120, 35, 685, 110), txtDialogue);
     GUI.EndGroup();
     GUI.EndGroup();
 }
 
 function TextDialogue()
 {
     txtDialogue = "";
     checkPunct = false;
     for(i = linePos; i < (linePos + 1); i++)
     {
         for(j = 0; j < lines[i].Length; j++)
         {
             txtDialogue = txtDialogue + lines[i].Substring(j, 1);
             
             //Enable and Disable autotype based on if the line is finished typing or not.
             if(j == lines[i].ToCharArray().Length - 1)
                 autotype = true;
             else
                 autotype = false;
             //
             
             yield WaitForSeconds(delay);
             if(lines[i].Substring(lines[i].Length - 2, 1).Equals(".") 
             || lines[i].Substring(lines[i].Length - 2, 1).Equals("!") 
             || lines[i].Substring(lines[i].Length - 2, 1).Equals("?")) checkPunct = true;
         }
         
         if(checkPunct == true)
         {
             i = i + 1;
             break;
         }
         
         txtDialogue = txtDialogue + "\n";
     }
     linePos = i;
 }
     
 function TextDialogue2()
 {
     yield TextDialogue();
 }
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I need help with my Door Switch Configuration. 1 Answer
attract particles 3 Answers
How to access variable from another function? 2 Answers
Every 0.5 sec there is a 10% chance of a target spawning. 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                