The primary thing lagging my game at the moment is list.Contains, are there any alternatives I can use to reduce the ms spike it’s causing? As you can see, each List.Contains here is generating about .25ms. There are 7047 list.Contains being called which leads us to over 1000 ms.
This is the script causing the issue, it’s part of my A* Pathfinding and you can see the three list.Contains towards the bottom.
public List<Node> FindPath(Vector3 a_StartPos, Vector3 a_TargetPosition)
{
Node startNode = grid.NodeFromWorldPosition(a_StartPos);
Node targetNode = grid.NodeFromWorldPosition(a_TargetPosition);
startNode.gCost = 0;
openList.Clear();
closedList.Clear();
openList.Add(startNode);
int x = 0;
while (openList.Count > 0)
{
x++;
Node currentNode = openList[0];
for (int i = 1; i < openList.Count; i++)
{
if (openList.fCost < currentNode.fCost || openList_.fCost == currentNode.fCost && openList*.hCost < currentNode.hCost)
{
_currentNode = openList*;
}
}
openList.Remove(currentNode);
closedList.Add(currentNode);
if (currentNode == targetNode)
{
finalPath.Clear();
Node curNode = targetNode;
while (curNode != startNode)
{
finalPath.Add(curNode);
curNode = curNode.parent;
}
finalPath.Reverse();
GetFinalPath(startNode, targetNode);
return finalPath;
}
neighboringNodes.Clear();
foreach (Node neighborNodes in grid.GetNeightboringNodes(currentNode, neighboringNodes))
{
if ((!neighborNodes.isWall && !neighborNodes.isOccupied) || closedList.Contains(neighborNodes))
{
continue;
}
int moveCost = currentNode.gCost + GetGetManhattenDistance(currentNode, targetNode);
if (moveCost < neighborNodes.fCost || !openList.Contains(neighborNodes))
{
neighborNodes.gCost = moveCost;
neighborNodes.hCost = GetGetManhattenDistance(neighborNodes, targetNode);
neighborNodes.parent = currentNode;
if (!openList.Contains(neighborNodes))
{
openList.Add(neighborNodes);
}
}
}
}
return null;
}
Thank you for any help!