我已经研究了这个问题,并且我已经尝试按照之前几个问题(例如这个)中的说明修复代码,但它仍然无法正常工作。虽然我必须说我检查的所有答案都是 2009-2010 年的,所以它们可能已经过时了。
这是罪魁祸首的代码:
foreach(Entity player in players)
{
if(player.actions.Count > 0)
{
Entity temp = player;
player.isDoingAction = true;
Debug.Log(player.name + " started action");
player.actions.Dequeue().Execute(() => { temp.isDoingAction = false; Debug.Log(temp.name + " finished"); });
}
}
Run Code Online (Sandbox Code Playgroud)
这将打印以下内容:
Player1 started action
Player2 started action
Player2 finished
Player2 finished
Run Code Online (Sandbox Code Playgroud)
什么时候应该打印:
Player1 started action
Player2 started action
Player1 finished
Player2 finished
Run Code Online (Sandbox Code Playgroud)
或者类似的东西。
此代码在 Unity 协程函数中运行。
代码中的一个更大的片段:
游戏管理器
private IEnumerator RunTurn()
{
...
...
...
for(int i = 0; i < phases; i++)
{
//Assign action to each player
foreach(Entity player in players)
{
if(player.actions.Count > 0)
{
Entity temp = player;
player.isDoingAction = true;
Debug.Log(player.name + " started action");
player.actions.Dequeue().Execute(() => { temp.isDoingAction = false; Debug.Log(temp.name + " finished"); });
}
}
//Wait for each player to finish action
foreach(Entity player in players)
{
while(player.isDoingAction == true)
{
Debug.Log("Waiting for " + player.name);
yield return null;
}
}
}
...
...
...
}
Run Code Online (Sandbox Code Playgroud)
动作.cs
public override void Execute(System.Action callback)
{
Move(callback);
}
private void Move(System.Action callback)
{
...
...
...
//Move target entity
target.MoveToPosition(newPosition, mSpeed, callback);
target.location = newLocation;
...
...
...
}
Run Code Online (Sandbox Code Playgroud)
实体.cs
public void MoveToPosition(Vector3 position, float speed, System.Action callback)
{
StartCoroutine(CoMoveToPosition(position, speed, callback));
}
//Move to position
private IEnumerator CoMoveToPosition(Vector3 position, float speed, System.Action callback)
{
while(position != transform.position)
{
transform.position = Vector3.MoveTowards(transform.position, position, speed * Time.deltaTime);
yield return null;
}
//Move finished so use callback
callback();
}
Run Code Online (Sandbox Code Playgroud)
解决方案
事实证明,Unity 中存在一个带有协程和匿名 lambda 回调的错误。查看此链接了解更多信息。
工作代码:
foreach(Entity player in players)
{
if(player.actions.Count > 0)
{
player.isDoingAction = true;
Debug.Log(player.name + " started action");
System.Func<Entity, System.Action> action = new System.Func<Entity,System.Action>(p =>
new System.Action(() => { p.isDoingAction = false; Debug.Log(p.name + " finished"); }));
player.actions.Dequeue().Execute(action(player));
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式捕获该值:
var action = new Func<Entity, Action>(p =>
new Action(() => { p.isDoingAction = false; Debug.Log(p.name + " finished")); })(player);
player.actions.Dequeue().Execute(action);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
824 次 |
最近记录: |