Hello Everybody!
So far I’ve been under the impression that if something breaks the code instantly stops working, but I am having a bit of trouble atm and am not getting an error message at all.
I will try to detail my problem a bit. I work with a grid-map (Hexes to be specific).
I have the following functions in my “Hex” Class:
public List<Hex> GetAllNeighbours()
{
if(neighbourDictionary == null){
FindNeighbours();
}
return neighbourDictionary.Values.ToList();
}
public void FindNeighbours()
{
neighbourDictionary = new Dictionary<(int,int), Hex>();
foreach (var direction in directions)
{
neighbourDictionary[direction] = hexMap.GetHexAtGridLocation(Q + direction.Item1, R + direction.Item2);
}
}
Directions is an array of tuples that determine the axial directions (using axial/cube corrdinates)
The later creates a dictionary of neighbours. Sometimes however a Hex won’t have 6 neighbours so some of the entries will be “null” instead. And I guess this is were the trouble comes from.
Now I have a function in a “HexMap” MonoBehaviour that calls GetNeighbours() and does something with the List of Hexes. It is currently called from “Update” with a button press. That function looks something like this:
public void highlight_targetable_Hexes(){
targetableHexes = hexDictionary[(1,1)].GetAllNeighbours(); ///(1,1) is just a position on the hexMap
foreach (var hex in targetableHexes)
{
if (hex != null)
{
///Replacement for code
Debug.Log("Working");
}
}
}
If I do this on a hex-tile that has 6 neighbours it works perfectly fine and continues running afterward. However when I use this on a hex with less than 6 neighbours I run into trouble.
The thing that is most odd to me is, that the code does not immediately break when confronted with the first “null” value. In fact it prints the correct amount of “Working” messages and even Debug messages put at the end of the faulty method. However after completing the method, the game just completely stalls and breaks (animations stop, input doesn’t work, I guess the entire game breaks down).
Since no error message a is shown and due to the fact that I always though a faulty method would stop working immediatley I am kind of confused right now.