A*在向成本排序字典中添加节点时,Pathfinding AI处于冻结状态
本文关键字:AI Pathfinding 于冻结 状态 冻结 节点 排序 添加 字典 | 更新日期: 2023-09-27 18:20:07
我正试图在一个简单的四向移动网格上构建一个基于A Star Pathfinding的AI,然而每当我试图将节点添加到比较字典中并对其进行排序时,它都会在dictionary.add上崩溃。我不完全确定为什么,有人能发现这个缺陷吗?
void DirectionFinder()
{
// Find Target Point //
GetBoardPosition();
BuildOrReBuildPawnRefBoard();
bool IsTrue = false;
int Serial = lastSquare.Location.Serial;
// Get the Serial Keys of the nodes that are neighboring this node //
bool TestNode = NodesClosed.ContainsKey(Serial);
Dictionary<int, Node> SortArray = new Dictionary<int, Node>();
SortArray.Clear();
if (TestNode)
{
Node ScanNode = NodesClosed[Serial];
// Get the Number of Neighbors I have //
int TestNumNeighbors = ScanNode.NeighborNodes.Count;
// Set up a Loop that will go through these nodes and get the lowest-scored movement node //
for (int loop = 0; loop < TestNumNeighbors; loop++)
{
int ScanNodePointX = ScanNode.NeighborNodes[loop].X;
int ScanNodePointY = ScanNode.NeighborNodes[loop].Y;
int ScanTestSerial = System_GameController.Instance.NodeArray[ScanNodePointX, ScanNodePointY].Location.Serial;
bool IsNodeOpen = NodesOpen.ContainsKey(ScanTestSerial);
if (IsNodeOpen)
{
Debug.Log("This Node is Open:" + ScanTestSerial);
Node CompareNode = NodesOpen[ScanTestSerial];
int CostOfNode = CompareNode.TotalCost;
SortArray.Add(CostOfNode, CompareNode);
}
}
}
}
它一直到"CostOfNode"。这很管用。。。。然后在尝试添加到SortArray时中断。
它在Dictionary.add上崩溃了。我不完全确定为什么,有人能发现这个缺陷吗?
int CostOfNode = CompareNode.TotalCost;
SortArray.Add(CostOfNode, CompareNode);
如果添加到字典中的两个不同节点的CostOfNode
相同,则第二个节点会导致崩溃。
参见字典。添加:
争论异常
字典中已存在具有相同关键字的元素。