Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Qvih · Jul 24, 2013 at 02:40 AM · javascriptfunction

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();
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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();
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Qvih · Jul 24, 2013 at 03:44 AM 1
Share

Yey! It worked. Thank you very much! :)

avatar image clunk47 · Jul 24, 2013 at 03:50 AM 1
Share

You're very welcome.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges