I am stuck at an error that I can understand but cannot fix, it's a CS0123 error where parameters between a method and a delegate do not match. But I looked at my code and it seems to be the right type so I'm confused and ask for your help since I'm learning more advanced C# with this project. This project is about to generate an hex grid and then process an A* pathfinding on it between two hex tiles. I used this serie of written tutorials even if it's an old one and had to refresh a bit the code to make it work on Unity 5 and more recent version of C# .NET (I guess).
Here are the errors :
Assets/Scripts/GridManager.cs(151,39): error CS0123: A method or delegate 'GridManager.calcDistance(Tile)' parameters do not match delegate 'System.Func()' parameters
Assets/Scripts/GridManager.cs(153,17): error CS1502: The best overloaded method match for 'GridManager.DrawPath(System.Collections.Generic.IEnumerable)' has some invalid arguments
Assets/Scripts/GridManager.cs(153,17): error CS1503: Argument '#1' cannot convert 'object' expression to type 'System.Collections.Generic.IEnumerable'
I'm pretty sure that the two lasts are there only because of the first because it does not recognize the right var type that should be a Tile.
I hope you'll be able to help me and explain me what I did wrong. I think I do not entirely what's going on there.
Thank you in advance !
Here is some of my modified code but it's basically the same as the tutorial :
GridManager.cs :
double calcDistance(Tile tile)
{
Tile destTile = destTileTB.tile;
float deltaX = Mathf.Abs(destTile.X - tile.X);
float deltaY = Mathf.Abs(destTile.Y - tile.Y);
int z1 = -(tile.X + tile.Y);
int z2 = -(destTile.X + destTile.Y);
float deltaZ = Mathf.Abs(z2 - z1);
return Mathf.Max(deltaX, deltaY, deltaZ);
}
private void DrawPath(IEnumerable<Tile> path)
{
if (this.path == null)
this.path = new List<GameObject>();
this.path.ForEach(Destroy);
this.path.Clear();
GameObject lines = GameObject.Find("Lines");
if (lines == null)
lines = new GameObject("Lines");
foreach (Tile tile in path)
{
var line = (GameObject)Instantiate(Line);
Vector2 gridPos = new Vector2(tile.X + tile.Y / 2, tile.Y);
line.transform.position = calcWorldCoord(gridPos);
this.path.Add(line);
line.transform.parent = lines.transform;
}
}
public void generateAndShowPath()
{
if (originTileTB == null || destTileTB == null)
{
DrawPath(new List<Tile>());
return;
}
Func<Tile, Tile, double> distance = (node1, node2) => 1;
var path = PathFinder.FindPath(originTileTB.tile, destTileTB.tile,
distance, calcDistance); //error is here
DrawPath(path);
}
PathFinder.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public static class PathFinder
{
public static Path<Node> FindPath<Node>(
Node start,
Node destination,
Func<Node, Node, double> distance,
Func<Node> estimate)
where Node : IHasNeighbours<Tile>
{
var closed = new HashSet<Node>();
var queue = new PriorityQueue<double, Path<Node>>();
queue.Enqueue(0, new Path<Node>(start));
while (!queue.IsEmpty)
{
var path = queue.Dequeue();
if (closed.Contains(path.LastStep))
continue;
if (path.LastStep.Equals(destination))
return path;
closed.Add(path.LastStep);
foreach (Node n in path.LastStep.Neighbours)
{
double d = distance(path.LastStep, n);
var newPath = path.AddStep(n, d);
queue.Enqueue(newPath.TotalCost + estimate(n), newPath);
}
}
return null;
}
}