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
0
Question by XGundam05 · May 23, 2013 at 04:44 PM · javascriptcoroutineyieldcallback

yield behaves oddly with anonymous functions

I'm implementing a callback based in-game cutscene system. When developing the dialog text portion of the system, I ran into some unexpected behavior when using yield in anonymous functions. I have been unable to figure out why the issue described below is caused. I apologize for the amount of code shown beforehand. Code in Unityscript. showText, Delay and foo all perform a yield return. I can easily work around the issue, but I am quite curious was to why it even occurs.

I would like to know why the following work:

 function doDialog(){
     showText("Hi there", function(){
         showText("Hi back");
     });
 }

 function doDialog(){
     showText("Hi there", function(){
         foo();
         Delay(1, function(){
             showText("Hi back");
         });
 }

While the following only make the first showText call:

 function doDialog(){
     showText("Hi there", function(){
         Delay(1, function(){
             showText("Hi back");
         });
     });
 }

 function doDialog(){
     showText("Hi there", function(){
         yield WaitForSeconds(1);
         showText("Hi back");
     });
 }

I don't quite get why the latter methods don't work. Especially the first, as the only difference between it and the second example in the former set is the inclusion of the call to foo() which is simply foo() { yield; }

Where the test class/script structure is such:

 private var isShown : boolean;
 private var __text : String;

 function Start(){ isShown = false; }

 function Update(){
     if(!isShown){
         isShown = true;
         doDialog();
     }
 }

 function OnGUI(){
     GUI.BeginGroup(new Rect(10,10,200,200));
         GUILayout.Box(__text);
     GUI.EndGroup();
 }

 function foo(){ yield; }
 
 function Delay(_seconds : float, _func : Function){
     if(_func != null){
         yield WaitForSeconds(_seconds);
         _func();
     }
 }

 function showText(_text : String){
     showText(_text, null);
 }

 function showText(_text : String, _func : Function){
     var i : int;
     var _tmp : String = "";

     __text = "";
     for(i = 0; i < _text.Length; i++)
         __text += " ";
     __text += "\r";

     for(i = 0; i < _text.Length; i++){
         while(_text[i] == " ")
             _tmp += _text[i++];
         
         _tmp += _text[i];
         __text = _tmp;
         for(var j : int = i + 1; j < _text.Length; j++)
             __text += " ";
         __text += "\r";

         yield WaitForSeconds(0.05);
     }

     if(_func != null)
         _func();
 }
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

Answer by XGundam05 · May 25, 2013 at 04:40 AM

Well, after some poking, I believe I can answer my own question. There are a couple things here:

  1. Anonymous functions cannot return an IEnumerator. While it would be awesome, apparently anonymous functions are the second most compiler intensive processes...with IEnumerators being the first. (Or at least very close)

  2. Because Unityscript appears to be interpreted and then compiled, doing too many odd things with anonymous functions and yield in Unityscript appears to confuse the compiler. And so it doesn't do it's magical insertion of StartCoroutine.

To get around this? Just went with C#, that way I know what everything is explicitly doing. I had originally wanted to write the cutscene scripts in, well, Unity...script. But, Lambdas look nicer for anonymous functions, C# supports closures, and it works.

Comment
Add comment · 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

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

13 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

Related Questions

Unityscript Coroutine Not Running When Called 1 Answer

yield works, but yield WaitForSeconds does not? 1 Answer

Coroutoutine doesnt work properly. 3 Answers

Getting an error when trying to yield 2 Answers

What am I doing wrong with IEnumerator? 1 Answer


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