Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by Ardenian · Feb 10, 2017 at 07:24 PM · coroutinefunctionyielditerateexpression

[C#] How to write a generic yield return WaitForCondition with an expression as argument ?

I wondered how one could write something like a yield return new WaitForCondition which allows you to wait until a condition is matched. I thought about making it with a helper function and a while - loop but I am not very satisfied with it, since it requires an unqiue WaitForCondition for each usage, as well as reference to a field and it does not allow expressions, like in if - statements, they are converted into a value, not preserving the expression as is.

In the end, I would like to be able to do something like this:

 public class MyClass{
     bool myBool = false;
     public IEnumerator MyWait(){
         yield return new WaitForCondition(myBool == true);
         // doSomething
     }
 }

I do know that in this example, I could instead write it similar to the accepted solution to this question: Waiting for Input via Coroutine

However, on purpose, I would like to require a bool expression as argument, no matter whether it is yield return new WaitForCondition( b == true );or yield return StartCoroutine(WaitForCondition(b == true));The examples might be misleading in terms of usability, imagine more complex bool expressions.

How would I achieve this ? I do know there are workarounds using Properties, but I would like to force this way. The main problem for me is preserving the expression as is instead of converting it to a value, as one is waiting for a condition to be matched, hence requiring to continuously checking for it.

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

3 Replies

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

Answer by jdean300 · Feb 10, 2017 at 08:52 PM

This already exists: https://docs.unity3d.com/ScriptReference/WaitUntil.html

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 jdean300 · Feb 10, 2017 at 09:09 PM 1
Share

Just to explain things a bit more - you cannot pass a statement like b == true into a function and expect it to be evaluated multiple times - the statement is evaluated once and then given to the function. If you want a function to act based on some passed in argument that needs to be evaluated multiple times, you have to pass a lambda.

A lambda is fancy speak for a function. $$anonymous$$ore precisely it is a function with an execution context(local variables and such) wrapped up into one. What WaitUntil does is take a lambda that you give it that returns a bool and calls it every frame. When it returns true, WaitUntil breaks.

Here is an example of using WaitUntil with a typical function (alan$$anonymous$$emp already posted a lambda example):

 private IEnumerator LookAt$$anonymous$$e() {
   // pass a function to WaitUntil
   yield return new WaitUntil(BIsTrue);
 }
 
 // this function will be called by WaitUntil every frame
 private bool BIsTrue() { return b == true; }

avatar image Ardenian jdean300 · Feb 14, 2017 at 12:46 PM 0
Share

Thank you very much! I hanged myself a bit with the term "condition", wondered why Unity would not have something like this as default. Great to know this exists! Also, a great thank you for your explanation!

avatar image
0

Answer by alankemp · Feb 10, 2017 at 08:52 PM

You can use WaitUntil to do this:

 yield return new WaitUntil( () => b == true );

Comment
Add comment · Show 1 · 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 Ardenian · Feb 14, 2017 at 12:47 PM 0
Share

Thank you!

avatar image
0

Answer by dan_ginovker · Feb 16 at 09:42 AM

If you're like me and want a custom implementation of WaitUntil or WaitForSeconds, check out CustomYieldInstruction:

https://docs.unity3d.com/ScriptReference/CustomYieldInstruction.html

https://forum.unity.com/threads/writing-a-custom-class-yieldinstruction-to-wait-for-frames-or-seconds-depending-on-the-class-params.448925/

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

7 People are following this question.

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

Related Questions

Coroutine doesn't continue after yield another coroutine 0 Answers

When to use IEnumerator ? 1 Answer

Coroutine: delay transform rotation but start transform movement immediately 2 Answers

Designing a dialogbox that displays message, returns a custom object and waits for user response 0 Answers

The Legend of the waiting for function to end! Still no answer for that, Please Help 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