Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by RomanLed · Nov 01, 2016 at 07:53 PM · functionparameter

How to pass Predicate to a Coroutine?

I'm trying to send a bool function as a parameter in a coroutine and i learned that using predicate is the solution to send bools (i think?....) however I'm unable to get it working because I'm not understanding what the "object" parameter is. It isn't the class?

  IEnumerator Pause( Predicate<WhatsThisParameter?> condition )
     {
         while (!condition)
         {
             yield return null;
         }       
     }
Comment
Add comment · Show 4
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 TreyH · Nov 01, 2016 at 08:13 PM 0
Share

As in the function would be evaluated multiple times in the coroutine?

avatar image RomanLed TreyH · Nov 01, 2016 at 08:41 PM 0
Share

correct, so i could send different bool functions to the condition parameter and evaluate them multiple times during the while statement.

avatar image TreyH RomanLed · Nov 01, 2016 at 10:41 PM 0
Share

is that function dynamically created on the fly, or is it a member function for some class?

If it's part of a component script, you could just pass that component's instance. I'm having trouble imagining a situation where the best-practice solution for this situation requires a reference to the function itself.

Show more comments

1 Reply

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

Answer by Bunny83 · Nov 01, 2016 at 11:30 PM

The generic Predicate type is a special delegate type that always returns a boolean and takes a single generic parameter. If you just want a delegate that takes no parameter at all you should simply use System.Func<bool> instead.

Also a delegate is a "method pointer" so you have to actually execute it to get the return value

 IEnumerator PauseWhile( System.Func<bool> condition )
 {
     if (condition == null)
         yield break;
     while (!condition())
         yield return null;
 }

This can be used like this:

 yield return StartCoroutine(PauseWhile(()=>YourConditionHere));



A Predicate always takes a parameter. For example:

 Predicate<int> condition = (p) => p > 10
 
 if(condition(5))  // returns "false"
 
 if(condition(11)) // returns "true"

The type Predicate<int> would be equivalent to the type Func<int, bool>. So it's a delegate to a method that takes an int parameter and returns a bool.

Keep in mind that you can assign any kind of method that matches the delegate signature, not only anonymous methods / lambda expressions:

 private bool Test(int aParam)
 {
     return aParam == 3;
 }
 
 Predicate<int> condition = Test;
 
 condition(3) // --> returns "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 RomanLed · Nov 01, 2016 at 11:54 PM 0
Share

Perfect! Thank you very much, I actually tried func<> and now realize it wasn't working initially because i didn't add the parenthesis to execute the function. Thank you for explaining predicates too. Awesome answer! $$anonymous$$uch appreciated!

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

60 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 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 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 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

Function as parameter? 1 Answer

return value of a function passed to another function as a parameter 0 Answers

Why pass parameters into a function? 2 Answers

i cannot pass through game object data type on Photon rpc function why? 1 Answer

Make Unity know which target was hit first. 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