本文介绍了使用C#5异步等待的东西,执行过一些游戏帧数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的儿子是编写一个简单的RPG游戏,有一些非玩家角色(NPC又名的)。每个NPC都有一个关联的剧本,从而控制其行为。我们将使用一个小型自定义脚本语言来编写这些行为,但现在我不知道这是否会在C#5 /异步得到更好的实现。 隔空很简单的例子,假设人大的只是两点之间走我想这将是很好写这样的事情之一: 而(真) {等待WalkTo(100,100); 等待WalkTo(200,200); } 的 WalkTo方法将是处理一切与之间行走做一个异步方法这两个点,这是否超过了一些从游戏循环帧。这不是一个阻塞方法可以被卸载到一个后台线程。 而这正是我坚持......我一直没能找到使用异步任何例子/等待以这种方式,但现在看来,这将是完美的。 想法? 下面是想我做一些很粗糙的伪代码: 类NpcBase { //从游戏循环公共无效的onUpdate调用(双elapsedTime) { //移动NPC 。 。 。 //抵达目的地? 如果(到达) { //我如何触发任务完成? _currentTask.MarkComplete(); } } //异步方法,通过全国人民代表大会称为脚本公共异步任务WalkTo(INT X,INTÿ ) { //存储新的目标位置 //返回将触发的时候走的是完成 _currentTask任务对象= LT;东西??> 返回_currentTask; } 任务_currentTask; } 解决方案 好吧,这听起来像一个选择会是有一个 TaskCompletionSource ,在游戏的每一帧。然后,您可以等待 工作从 WalkTo ,并设置结果的OnUpdate : 私人TaskCompletionSource<双> currentFrameSource; //从游戏循环公共无效的OnUpdate调用(双elapsedTime) { ... VAR previousFrameSource = currentFrameSource; currentFrameSource =新TaskCompletionSource<双>(); //这将触发所有的延续... previousFrameSource.SetResult(elapsedTime); } //异步方法,通过全国人民代表大会称为脚本公共异步任务WalkTo(INT X,int y)对 { //商店新的目标位置,而(/ *我们现在还没有* /) {双currentTime的等待= currentFrameSource.Task; //将} } 我不知道如何高效,这将是无可否认...但它应该工作。 My son is writing a simple RPG game that has a number of non-player characters (aka NPC's). Each NPC has an associated "script" that controls its behaviour. We were going to use a mini custom script language to write these behaviours but I'm now wondering if this would be better done in C#5/Async.Taking a really simple example, suppose one of the NPC's just walks between two points I'm thinking it would be nice to write something like this:while (true){ await WalkTo(100,100); await WalkTo(200,200);}The WalkTo method would be an async method that handles everything to do with walking between the two points and does this over a number of frames from the game loop. It's not a blocking method that can be off-loaded to a background thread.And this is where I'm stuck... I haven't been able to find any examples using async/await in this manner, but it seems it would be perfect for it.Ideas?Here's some very rough pseudo code for what I'd like to do:class NpcBase{ // Called from game loop public void onUpdate(double elapsedTime) { // Move the NPC . . . // Arrived at destination? if (Arrived) { // How do I trigger that the task is finished? _currentTask.MarkComplete(); } } // Async method called by NPC "script" public async Task WalkTo(int x, int y) { // Store new target location // return a task object that will be "triggered" when the walk is finished _currentTask = <something??> return _currentTask; } Task _currentTask;} 解决方案 Okay, it sounds like one option would be to have a TaskCompletionSource for each frame of the game. You can then await the Task from WalkTo, and set the result in OnUpdate:private TaskCompletionSource<double> currentFrameSource;// Called from game looppublic void OnUpdate(double elapsedTime){ ... var previousFrameSource = currentFrameSource; currentFrameSource = new TaskCompletionSource<double>(); // This will trigger all the continuations... previousFrameSource.SetResult(elapsedTime);}// Async method called by NPC "script"public async Task WalkTo(int x, int y){ // Store new target location while (/* we're not there yet */) { double currentTime = await currentFrameSource.Task; // Move }}I'm not sure how efficient this will be, admittedly... but it should work. 这篇关于使用C#5异步等待的东西,执行过一些游戏帧数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 08:22
查看更多