{"id":8387,"date":"2022-08-30T11:36:00","date_gmt":"2022-08-30T08:36:00","guid":{"rendered":"https:\/\/eran.geek.co.il\/wp\/?p=8387"},"modified":"2024-01-30T16:17:14","modified_gmt":"2024-01-30T14:17:14","slug":"things-ive-learned-waituntil-waitwhile","status":"publish","type":"post","link":"https:\/\/eran.geek.co.il\/wp\/archives\/8387","title":{"rendered":"Unity Tidbit: WaitUntil, WaitWhile"},"content":{"rendered":"\n<p>If you&#8217;ve been working with Unity for any sort of while you&#8217;ve probably used Coroutines before. They&#8217;re not exactly the bread and butter of using Unity but they&#8217;re like the jam or honey you put immediately after. You&#8217;ve probably used a lot of &#8220;yield return null&#8221; to wait for the next frame before resuming execution? I bet you even have used a &#8220;yield return null&#8221; loop to check a predicate every frame before continuing. I&#8217;ve done it myself. I&#8217;ve even written a repeating coroutine helper that uses null to check a predicate before running a given action.<\/p>\n\n\n\n<p>Well&#8230; stop that.<\/p>\n\n\n\n<p>Recently (and by &#8216;recently&#8217;, I mean at least a year) Unity now has two new conditions you can use in coroutines: WaitUntil and WaitWhile and they do exactly what you think they do.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yield return new WaitUntil(()=&gt;Func&lt;bool&gt;);\nyield return new WaitWhile(()=&gt;Func&lt;bool&gt;);<\/code><\/pre>\n\n\n\n<p>Just use any of these two instead of your usual &#8220;yield return&#8221; and put in a function (even a lambda) that returns a boolean then the coroutine will handle the checking by itself. It&#8217;ll check every frame if the predicate is fulfilled and will break if it is or isn&#8217;t, respectively.<\/p>\n\n\n\n<p>The underlying code this represents is exactly the same like you wrote yourself before but now your code can be a lot more elegant. You&#8217;re welcome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;ve been working with Unity for any sort of while you&#8217;ve probably used Coroutines before. They&#8217;re not exactly the bread and butter of using Unity but they&#8217;re like the jam or honey you put immediately after. You&#8217;ve probably used a lot of &#8220;yield return null&#8221; to wait for the next frame before resuming execution? I bet you even have used a &#8220;yield return null&#8221; loop to check a predicate every frame before continuing. I&#8217;ve done it myself. I&#8217;ve even written a repeating coroutine helper that uses null to check a predicate before running a given action. Well&#8230; stop that. Recently (and by &#8216;recently&#8217;, I mean at least a year) Unity now has two new conditions you can use in coroutines: WaitUntil and WaitWhile and they do exactly what you think they do. Just use any of these two instead of your usual &#8220;yield return&#8221; and put in a function (even a lambda) that returns a boolean then the coroutine will handle the checking by itself. It&#8217;ll check every frame if the predicate is fulfilled and will break if it is or isn&#8217;t, respectively. The underlying code this represents is exactly the same like you wrote yourself before but now your code can be a lot more elegant. You&#8217;re welcome.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,1,27],"tags":[157,158,144,153],"class_list":["post-8387","post","type-post","status-publish","format-standard","hentry","category-it","category-_","category-programming","tag-c","tag-programming","tag-unity","tag-work"],"_links":{"self":[{"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/posts\/8387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/comments?post=8387"}],"version-history":[{"count":6,"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/posts\/8387\/revisions"}],"predecessor-version":[{"id":8905,"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/posts\/8387\/revisions\/8905"}],"wp:attachment":[{"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/media?parent=8387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/categories?post=8387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/tags?post=8387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}