这个问题与使用C# 5异步等待一些在多个游戏帧上执行的内容有关。
背景
当Miguel de Icaza首次在Alt-Dev-Conf 2012上展示C# 5异步框架用于游戏时,我非常喜欢使用async
和await
来处理游戏中的“脚本”(这么说是因为它们是用c#编写的,在我的情况下,即时编译但无论如何都是编译)。
即将发布的Paradox3D游戏引擎似乎也依赖于异步框架来处理脚本,但从我的角度来看,这种想法和实现之间存在真正的差距。
在相关链接问题中,有人使用await
让NPC执行一系列指令,而游戏的其余部分仍在运行。
想法
我希望更进一步,允许NPC同时执行多个动作,同时以顺序方式表达这些动作。类似于:
class NonPlayableCharacter
{
void Perform()
{
Task walking = Walk(destination); // Start walking
Task sleeping = FallAsleep(1000); // Start sleeping but still walks
Task speaking = Speak("I'm a sleepwalker"); // Start speaking
await walking; // Wait till we stop moving.
await sleeping; // Wait till we wake up.
await speaking; // Wait till silence falls
}
}
为了实现这一点,我使用了Jon Skeet的一篇非常精彩的答案,该答案来自于相关问题。
实现
我的玩具实现由两个文件组成,NPC.cs和Game.cs。 NPC.cs:
using System;
using System.Threading.Tasks;
namespace asyncFramework
{
public class NPC
{
public NPC (int id)
{
this.id = id;
}
public async void Perform ()
{
Task babbling = Speak("I have a superpower...");
await Speak ("\t\t\t...I can talk while talking!");
await babbling;
done = true;
}
public bool Done { get { return done; } }
protected async Task Speak (string message)
{
int previousLetters = 0;
double letters = 0.0;
while (letters < message.Length) {
double ellapsedTime = await Game.Frame;
letters += ellapsedTime * LETTERS_PER_MILLISECOND;
if (letters - previousLetters > 1.0) {
System.Console.Out.WriteLine ("[" + this.id.ToString () + "]" + message.Substring (0, (int)Math.Floor (Math.Min (letters, message.Length))));
previousLetters = (int)Math.Floor (letters);
}
}
}
private int id;
private bool done = false;
private readonly double LETTERS_PER_MILLISECOND = 0.002 * Game.Rand.Next(1, 10);
}
}
Game.cs:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace asyncFramework
{
class Game
{
static public Random Rand {
get { return rand; }
}
static public Task<double> Frame {
get { return frame.Task; }
}
public static void Update (double ellapsedTime)
{
TaskCompletionSource<double> previousFrame = frame; // save the previous "frame"
frame = new TaskCompletionSource<double> (); // create the new one
previousFrame.SetResult (ellapsedTime); // consume the old one
}
public static void Main (string[] args)
{
int NPC_NUMBER = 10; // number of NPCs 10 is ok, 10000 is ko
DateTime currentTime = DateTime.Now; // Measure current time
List<NPC> npcs = new List<NPC> (); // our list of npcs
for (int i = 0; i < NPC_NUMBER; ++i) {
NPC npc = new NPC (i); // a new npc
npcs.Add (npc);
npc.Perform (); // trigger the npc actions
}
while (true) { // main loop
DateTime oldTime = currentTime;
currentTime = DateTime.Now;
double ellapsedMilliseconds = currentTime.Subtract(oldTime).TotalMilliseconds; // compute ellapsedmilliseconds
bool allDone = true;
Game.Update (ellapsedMilliseconds); // generate a new frame
for (int i = 0; i < NPC_NUMBER; ++i) {
allDone &= npcs [i].Done; // if one NPC is not done, allDone is false
}
if (allDone) // leave the main loop when all are done.
break;
}
System.Console.Out.WriteLine ("That's all folks!"); // show after main loop
}
private static TaskCompletionSource<double> frame = new TaskCompletionSource<double> ();
private static Random rand = new Random ();
}
}
这是一个非常直接的实现!
问题
然而,它似乎没有按预期工作。
更准确地说,当NPC_NUMBER为10、100或1000时,我没有问题。但在10,000或以上时,程序不再完成,它会写出“speaking”的行一段时间,然后控制台上就没有其他内容了。 虽然我不打算在我的游戏中同时拥有10,000个NPC,但它们也不会写出愚蠢的对话,而是会移动、动画、加载纹理等等。因此,我想知道我的实现有什么问题,如果有任何修复的机会。
我必须说明的是,代码正在Mono下运行。此外,“有问题”的值可能在您的位置不同,可能是计算机特定的问题。如果问题似乎无法在.Net下重现,我将尝试在Windows下运行它。
编辑
在.Net中,它可以运行到1000000,尽管需要时间进行初始化,它可能是Mono特有的问题。 调试数据告诉我确实有未完成的NPC。不幸的是,还没有关于原因的信息。
编辑2
在Monodevelop下,无调试器启动应用程序似乎可以解决问题。然而,为什么能解决问题我不知道…结尾
我意识到这是一个非常冗长的问题,我希望你能花时间阅读它,我真的很想了解我做错了什么。
非常感谢您的帮助。