Question

I'm a budding programming enthusiast and game designer studying to finish my degree, and thus am still quite new in the world of programming. I've done a fair amount of JavaScript (actually UnityScript) and now am trying to dabble in C#. I have been following hakimio's tutorial of making a turn-based RPG in Unity, which is based on a hexagon grid using A* pathfinding. (http://tbswithunity3d.wordpress.com/)

My question is that I have followed his tutorial step-by-step up to the point of finishing the A* pathfinding scripts and assets, but have come across the error in Unity:

"error CS0308: The non-generic type `IHasNeighbours' cannot be used with the type arguments"

This is the code that sparks the error message, on the line public class Tile: GridObject, IHasNeighbours<Tile>:

using System.Collections.Generic;
using System;
using System.Linq;
using UnityEngine;

public class Tile: GridObject, IHasNeighbours<Tile>
{
public bool Passable;

public Tile(int x, int y)
    : base(x, y)
{
    Passable = true;
}

public IEnumerable AllNeighbours { get; set; }
public IEnumerable Neighbours
{
    get { return AllNeighbours.Where(o => o.Passable); }
}

public static List<Point> NeighbourShift
{
    get
    {
        return new List<Point>
        {
            new Point(0, 1),
            new Point(1, 0),
            new Point(1, -1),
            new Point(0, -1),
            new Point(-1, 0),
            new Point(-1, 1),
        };
    }
}
public void FindNeighbours(Dictionary<Point, Tile> Board, Vector2 BoardSize, bool EqualLineLengths)
{
    List<Tile> neighbours = new List<Tile>();

    foreach (Point point in NeighbourShift)
    {
        int neighbourX = X + point.X;
        int neighbourY = Y + point.Y;
        //x coordinate offset specific to straight axis coordinates
        int xOffset = neighbourY / 2;

        //if every second hexagon row has less hexagons than the first one, just skip the last one when we come to it
        if (neighbourY % 2 != 0 && !EqualLineLengths && neighbourX + xOffset == BoardSize.x - 1)
            continue;
        //check to determine if currently processed coordinate is still inside the board limits
        if (neighbourX >= 0 - xOffset &&
            neighbourX < (int)BoardSize.x - xOffset &&
            neighbourY >= 0 && neighbourY < (int)BoardSize.y)
            neighbours.Add(Board[new Point(neighbourX, neighbourY)]);
    }

    AllNeighbours = neighbours;
}
}

Any help or insight on how to get past this error would be greatly appreciated, I've struggled over this script for the past few days trying to get it to work, and can't proceed with the tutorial (and my project) with the error.

Thanks in advance everyone!

Aaron :)

Was it helpful?

Solution

The problem will be that IHasNeighbours is not a generic interface so you can't pass in a class to it the way you have passed the Tile class.

Either you need to modify your IHasNeighbours interface to make it generic or you need to take out that reference to the Tile class after it. The solution will depend on what you need your code to do. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top