- Home /
Code not consistently executing
The code in question is as follows:
for(var n : int in hex.neighbors)
{
Debug.Log("Entering neighbor : "+n);
//if neighbor is valid
if(n>-1)
{
var nDom : TerrainType= hexmap.GetHex(n).GetComponent(Hex).GetDominantTerrain();
Debug.Log(n+", Valid Neighbor. Dominant terrain: "+nDom.tName);
if(FindTerrain(nDom))
{
Debug.Log("Terrain already in list...");
for(var t : TerrainType in tDistribution)
{
if(t.tName == nDom.tName)
{
Debug.Log(nDom.tName+" was found. Increasing weight by "+hexWeight);
t.tWeight += hexWeight*remainder;
}
}
}
else
{
Debug.Log("Adding "+nDom.tName+" to the distribution.");
nDom.tWeight = hexWeight*remainder;
tDistribution.Add(nDom);
}
}
}
The FindTerrain() code is here:
function FindTerrain(tt : TerrainType) : boolean
{ Debug.Log("Entering FindTerrain()");
for(var t : TerrainType in tDistribution)
if(t.tName == tt.tName)
{
Debug.Log("Found "+tt.tName+". Exiting FindTerrain()");
return true;
}
Debug.Log(tt.tName+" not found. Exiting FindTerrain()");
return false;
}
Here is the scenario:
This is a snippet of code that executes when I "paint" a terrain type onto a hexagon on my map. The painting functionality executes from OnGUI() in an EditorWindow. This particular snippet is supposed to look at all of the neighboring hexagons (hex.neighbors) and incorporate the neighboing terrain types in the current hexagon's terrain distribution. The terrain distribution is just an array of TerrainType. When a neighbor actually exists (border hexagons won't have neighbors on all sides), it get's the neighbor's terrain type and calls FindTerrain() to see if that type is already in the list. If so, it increases the 'weight' (read as: frequency) of that terrain in the terrain distribution. If not, it adds that type to the terrain distribution and sets its weight.
The problem: Picture a hexagon and all of its neighbors (all valid) with terrain A. If I paint the middle hexagon a different terrain, the current terrain distribution is emptied, and the new terrain is added to the terrain distribution (so, now there is only this terrain in the distribution). The above code then executes (there's a little bit of the code I left out b/c it isn't relevant) to recalculate the rest of the distribution of the terrains in that hexagon. It works as it should for the first two neighbors. On the first neighbor, it checks the distribution and doesn't find the neighbor's terrain, so it adds it. Good. On the next neighbor, which has the same terrain as the first neighbor, it does find the second neighbor's terrain in the list (because it was added when it looked at the first neighbor). So, having found the second neighbor's terrain already in the list, it just increases the weight.
Now, for every subsequent neighbor, it executes fine right up until the if(FindTerrain(nDom)) line. For some reason, for each remaining neighbor, it just skips over the entire If...Else block and finishes out. No exceptions are thrown. Can anyone see what might be happening??