So, yes, I'm heretotalktoyouaboutTommygotcheeseandgenerators, and I'm reallyexcitedthatCrystalwentbeforemebecauseshealreadyexplainedsomeconceptsthat I'm gonnagointointhistalkaswell.
Now, youcanalsorun a generatorfrominsideofanothergenerator, andtodothat, you'regonnausetheyieldAsterixexpression I'm gonnacallit.
Yieldsstartcauseit's just a littlebiteasiertosay.
Buttoillustratethis, I have a generatorfunctioncalledouterand a generatorfunctioncalleddinner, andyou'llnoticethatouteriscallinginnerwiththeyieldstarexpression.
Youcanseeit's pausingatthefirstyieldstatementintheinnergenerator, andthevaluebeingreturnedisn't thegeneratoritself, butthevalue A fromtheinnergenerator.
Soyouoldstoreis a delegate, sowhenyoucallnextandencounters a yieldstarexpression, itdelegatesthegeneratortotherightofthatexpression, anditwillcontinuetodosoasyoucallnext, untilthatgeneratoriscomplete.
Youcanyieldallsortsof a sink, thingslikecallbackfunctionsandgeneratorsthemselvesandMaurUm, andthere's a reallygreatlibrarycalledCO.
Ifyouwanttoplayaroundwiththat, I reallyhighlyrecommendit.
CAroutinesarevery, verypowerful, andthereasonthey'resopowerfulisthattheyallowyoutothinksequentiallyabout a sinkcodebecausethecoroutineisyourcooperativepartnerinhandlingthemessybusinessofchaininganddealingwith a sinkcodeforyouinthebackground, youcanwriteyourcodeasifitransequentially.
Soforthesecondversionofthisgame, I rewrotealltheanimationswithpromisesandgenerators.
Sowhatyou'reseeinghereisactuallythesameidolanimation, butwithgeneratorsandpromisesandrunby a coroutine.
And I hadtwogoalswiththisrewrite.
I wantedtobeabletopauseanimations, and I wanttotakeadvantageofthinkingsequentiallyabout a sinkcode.
Sotodothat, I startedwithrewritingthefunctionthatdelaystheanimation.
Sothisisour A sinkfunction, anditdoesn't needtoholdtheanimationitself.
Itjustneedstodelaywith a promisethat's itssoleresponsibility.
We'llcreate a variabletosaythestatusofthegenerator, doneornot, andwillpresetinanimationtidal.
We'lluse a wildlooptocreate a neverendinggenerator.
Sothiswaythegameloopisgoingtocontinueforever.
Ifwehave a pendingusereventwillcancelthecurrentanimationanddelegatetotheeventusingYieldStarandwhatevertheevent, yieldswillbepassedtothecoverteam.
Otherwise, a secondwildloopwillhandlerunningtheanimation a wildloopinside a while.
Look, I wantyouunderstandthisisnutsaslongastheanimationisnotcompleteandtherearenopendingevents, thisloopwillyieldthegeneratorsvaluestothecoroutinetohandleandtheseairpromises, sotheCorpsroutineisgonnahandlethetraining.
Butiftheanimationisdoneorthereis a pendinguserevent, Loopisgoingtoterminatestoppingtheanimationgeneratorfromrunningagain.