1

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 :)

Chris
  • 27,210
  • 6
  • 71
  • 92
Aaron Goss
  • 13
  • 3
  • Hi Aaron. Just a quick note to say that I edited your post to fix the problem you commented on with needing the space to deal with `<>`. You should see when posting on the toolbar a "code" button. This will mark the text as code and make sure it shows all your text and formats it too. You've used it for the main bulk but it can also be used inline. – Chris May 31 '12 at 09:09

1 Answers1

1

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. :)

Chris
  • 27,210
  • 6
  • 71
  • 92
  • Thanks for the quick reply @Chris, but I'm still unsure as to how I can solve the problem. I tried removing the '' and then it started listing other errors of the same type with IEnumerator (*IEnumerable). I just don't know enough to be able to solve the issue... – Aaron Goss May 31 '12 at 09:42
  • Well, looking at this error if removing `` solved it (use backticks ` for marking codey things) then your interface was not generic. It may be that you need to go and read up on Generics to understand what they are, when you need them and when you should be putting on the generic type parameters (such as ``). For example `AllNeighbours` and `Neighbours` probably want to be `IEnumerable` rather than plain `IEnumerable`. If this is from your interface then your solution might be to modify your interface to be generic (post the code and we can take a look). – Chris May 31 '12 at 10:23
  • after looking around some more, I discovered that Aron Granberg's A* pathfinding package could be adapted to be used on a hex grid (by 'John' http://redclovergames.com/blog/?p=563), and furthermore I can adapt it to be used on mobile platform to utilise touch controls instead of mouse clicks, which is perfect for what I needed. Thanks for your help Chris :) – Aaron Goss May 31 '12 at 12:39