洞察探索open banking如何通过小程序容器技术助力金融企业实现数据安全和数字化转型
774
2022-10-26
Coroutines 一个在C#中运行嵌套协同程序的简单系统
Coroutines
A simple system for running nested coroutines in C#. Just drop Coroutines.cs into your project and you're ready to go.
What is a coroutine?
C# has a feature called "enumerators" which are functions that can be suspended during their execution. They do this by using yield return, rather than regular return. When you use yield, you effectively pause the function, and at any time you can resume it and it will continue after the most recent yield.
In this context, a "coroutine" is basically one of these functions, but wrapped up in a nice little package and a few extra features:
A container that will automatically update several coroutines for youA simple syntax for nesting coroutines so they can yield to othersThe ability to yield a floating point number, which will pause the routine for a desired amount of timeHandles to routines are provided to make tracking them easier
Example usage
To use the system, you need to create a CoroutineRunner and update it at regular intervals (eg. in a game loop). You also need to tell it a time interval.
CoroutineRunner runner = new CoroutineRunner();void UpdateGame(float deltaTime){ runner.Update(deltaTime);}
Now you can run coroutines by calling Run(). Here's a simple coroutine that counts:
IEnumerator CountTo(int num, float delay){ for (int i = 1; i <= num; ++i) { yield return delay; Console.WriteLine(i); }}void StartGame(){ //Count to 10, pausing 1 second between each number runner.Run(CountTo(10, 1.0f));}
When you yield a floating-point number, it will pause the coroutine for that many seconds.
You can also nest coroutines by yielding to them. Here we will have a parent routine that will run several sub-routines:
IEnumerator DoSomeCounting(){ Console.WriteLine("Counting to 3 slowly..."); yield return CountTo(3, 2.0f); Console.WriteLine("Done!"); Console.WriteLine("Counting to 5 normally..."); yield return CountTo(5, 1.0f); Console.WriteLine("Done!"); Console.WriteLine("Counting to 99 quickly..."); yield return CountTo(99, 0.1f); Console.WriteLine("Done!");}void StartGame(){ runner.Run(DoSomeCounting());}
You can also stop any running routines:
//Stop all running routinesrunner.StopAll();//Start a routine and store a handle to itCoroutineHandle myRoutine = runner.Run(SomeRoutine());//Stop a specific routinemyRoutine.Stop();
Other tips and tricks
A coroutine can run infinitely as well by using a loop. You can also tell the routine to "wait for the next update" by yielding null:
IEnumerator RunThisForever(){ while (true) { yield return null; }}
Coroutines are very handy for games, especially for sequenced behavior and animations, acting sort of like behavior trees. For example, a simple enemy's AI routine might look like this:
IEnumerator EnemyBehavior(){ while (enemyIsAlive) { yield return PatrolForPlayer(); yield return Speak("I found you!"); yield return ChaseAfterPlayer(); yield return Speak("Wait... where did you go!?"); yield return ReturnToPatrol(); }}
Sometimes you might want to run multiple routines in parallel, and have a parent routine wait for them both to finish. For this you can use the return handle from Run():
IEnumerator GatherNPCs(Vector gatheringPoint){ //Make three NPCs walk to the gathering point at the same time var move1 = runner.Run(npc1.WalkTo(gatheringPoint)); var move2 = runner.Run(npc2.WalkTo(gatheringPoint)); var move3 = runner.Run(npc3.WalkTo(gatheringPoint)); //We don't know how long they'll take, so just wait until all three have finished while (move1.IsPlaying || move2.IsPlaying || move3.IsPlaying) yield return null; //Now they've all gathered!}
Here is a more complicated example where I show how you can use coroutines in conjunction with asynchronous functions (in this case, to download a batch of files and wait until they've finished):
IEnumerator DownloadFile(string url, string toFile){ //I actually don't know how to download files in C# so I just guessed this, but you get the point bool done = false; var client = new WebClient(); client.DownloadFileCompleted += (e, b, o) => done = true; client.DownloadFileAsync(new Uri(url), toFile); while (!done) yield return null;}//Download the files one-by-one in syncIEnumerator DownloadOneAtATime(){ yield return DownloadFile("http://site.com/file1.png", "file1.png"); yield return DownloadFile("http://site.com/file2.png", "file2.png"); yield return DownloadFile("http://site.com/file3.png", "file3.png"); yield return DownloadFile("http://site.com/file4.png", "file4.png"); yield return DownloadFile("http://site.com/file5.png", "file5.png");}//Download the files all at once asynchronouslyIEnumerator DownloadAllAtOnce(){ //Start multiple async downloads and store their handles var downloads = new List
Why coroutines?
I use coroutines a lot in my games, as I find them great for organizing actor behavior and animations. As opposed to an async callback-based system, coroutines allow you to write your behaviors line-by-line, like how you would naturally write code, and result in very clean and easy to understand sequences.
There are good and bad times to use them, and you will get better at distinguishing this as you use them more. For many of my games, coroutines have been completely priceless, and have helped me organize and maintain very large and complicated systems that behave exactly in the order I wish them to.
NOTE: Not all languages have built-in support for coroutine systems like this. If you plan on porting your code to other languages, it may not be worth the pain of porting if your target language does not have a reliable means of implementing coroutines.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~