Weird behavior in script (Bug?)

So the code I’m dealing with is this:

bool[,] obstacleMap = new bool [mapSize.x, mapSize.y];
        showmap(obstacleMap);

        int obstacleCount = (int)(mapSize.x * mapSize.y * obstaclePercent);
        int currentObstacleCount = 0;

        Debug.Log("starting generation");
        for ( int i = 0; i < obstacleCount; i++ ) {
            Coord randomCoord = GetRandomCoord();
        
            if ( randomCoord != mapCenter && FloodFill(obstacleMap, currentObstacleCount ) ) {
                Debug.Log("coords at which obst will be placed:" + randomCoord.x + "," + randomCoord.y);
                showmap(obstacleMap);
               obstacleMap[randomCoord.x, randomCoord.y] = true;
                currentObstacleCount++;
            
                Vector3 obstaclePos = CoordToPosition(randomCoord.x, randomCoord.y);
        
                Transform newObstacle = Instantiate(obstaclePrefab, obstaclePos + Vector3.up * .5f, Quaternion.identity ) as Transform;
                newObstacle.parent = mapHolder;
                newObstacle.name = "Obstacle" + i;
            }
            }
        }

GetRandomCoord() just gets a random coord that is guaranteed to be on the map, and never repeats itself.
showmap() is just a quick debug function that prints obstacleMap in the log.
FloodFill works fine as far as i can tell, here it is anyways:

public bool FloodFill(bool[,] obstacleMap, int inaccessibleTiles ) {
        Coord[] nextDir = {
            new Coord(-1,0),
            new Coord(0,1),
            new Coord(1,0),
            new Coord(0,-1)
        }; //LEFT TOP RIGHT BOTTOM
        Queue<Coord> q = new Queue<Coord> ();

        bool[,] map = obstacleMap;
 
        int targetTiles = mapSize.x * mapSize.y - inaccessibleTiles;
        int currentTiles = 1;
        q.Enqueue(mapCenter);
        map[mapCenter.x, mapCenter.y] = true;

        while ( q.Count > 0 ) {
            Coord tile = q.Dequeue();
            Debug.Log("visited " + tile.x + tile.y + ", now looking at neighbours");
            foreach ( Coord nextDelta in nextDir ) {
                Coord next = tile + nextDelta;
                //Debug.Log("next is " + next.x + " " + next.y);
                if ( next.x >= 0 && next.x < mapSize.x && next.y >= 0 && next.y < mapSize.y ) {
                    Debug.Log("next (at "+next.x+","+next.y+") is inside the map. is occupied: " + map[next.x,next.y] );
                    if ( !map[next.x, next.y] ) {
                        q.Enqueue(next);
                        map[next.x, next.y] = true;
                        currentTiles++;
                    }
                }
            }
        }
        Debug.Log("currentTiles: " + currentTiles);
        Debug.Log("targetTiles: " + targetTiles);
        return currentTiles == targetTiles;
    }

edit: I’m an idiot and forgot to actually mention my issue:

after running obstacleMap[randomCoord.x, randomCoord.y] = true; the map is fully true in every position.

edit 2: its completely full even after running FloodFill() in the if.

please help as I’m losing my mind :slight_smile:

1 Like

A request for debugging assistance should include:

  • What problem are you trying to solve
  • What approach you are taking to solve it
  • What you expected to happen
  • What actually happened instead (and how that’s different)

I suppose your statement would be axiomatic if the map was 1x1.

Top script, what happens if you duplicate line 13 to be after line 14 as well… does every cell suddenly go true?

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.