0
\$\begingroup\$

I'm writing a script to randomly generate a maze, and I can't even get it off the ground, rip. I've written fractal generators so I'm no stranger to nested FOR loops. FOR x{ FOR y }
I'm using my same old tricks to generate the maze. To initialize it, I set the boundary of the Maze (NESW sides) to be walls like so:

// Create
List<Room> row = new List<Room>();
for(int i=0; i<width; i++)  // Columns (X-axis)
{   row.Clear();
    for(int j=0; j<depth; j++){ // Rows (Z-axis)
        row.Add(new Room(i, yOffset, j));

        if(i==width-1){ Debug.Log("SetE: i "+i+", j "+j);
        row[j].setE(-1); }
        if(i==0){ Debug.Log("SetW: i "+i+", j "+j);
        row[j].setW(-1); }
    }
    row[width-1].setN(-1);
    row[0].setS(-1);
    rooms.Add(row);
}
// Instantiate
for(int i=0; i<width; i++) for(int j=0; j<depth; j++)
{   // Room
    Instantiate(roomGO, new Vector3(i + transform.position.x,
                           yOffset, j + transform.position.z), transform.rotation);
    // North Side
    Instantiate(getWall(rooms[i][j],0), new Vector3(i + transform.position.x,
                                           yOffset, j + transform.position.z + .75f), transform.rotation);
    // East Side
    Instantiate(getWall(rooms[i][j],1), new Vector3(i + transform.position.x + .75f,
                                           yOffset, j + transform.position.z), transform.rotation);
    // South Side
    Instantiate(getWall(rooms[i][j],2), new Vector3(i + transform.position.x,
                                           yOffset, j + transform.position.z - .75f), transform.rotation);
    // West Side
    Instantiate(getWall(rooms[i][j],3), new Vector3(i + transform.position.x - .75f,
                                           yOffset, j + transform.position.z), transform.rotation);
}

My problem is here (*I think), Lines 7 to 10:

if(i==width-1){ Debug.Log("SetE: i "+i+", j "+j);
    row[j].setE(-1); }
if(i==0){ Debug.Log("SetW: i "+i+", j "+j);
    row[j].setW(-1); }

The positions are correct. I have no problems with the North and South sides.
The Western walls are not Instantiating. But the Eastern walls are Instantiating on every single row?
I'm trying to set all the West and East walls of the rooms with Coords (x=0/x=Width, z). The Debug.Log lines are printing the expected coordinates.
Question
Why aren't the Eastern and Western walls Instantiating properly? What is a better way to set the walls of my Maze?

\$\endgroup\$
3
  • 2
    \$\begingroup\$ "Is this Unity, C#, my Code, or my Computer?" Always assume it's your code. If there were a flaw in Unity or your computer so fundamental that they couldn't properly navigate a for loop, your computer would fail to boot, and no game would have ever shipped on the engine. So "this is someone else's fault" is not a plausible explanation here. \$\endgroup\$
    – DMGregory
    Commented Nov 2, 2022 at 20:03
  • \$\begingroup\$ What exactly is rooms? You're using a single instantiation for row. row gets cleared at the top of the first loop & added to rooms at the bottom. Unless rooms.Add(row) is making a deep copy (which wouldn't be my assumption) this looks like the steps of iteration n will always be wiping out the work of iteration n-1. \$\endgroup\$
    – Pikalek
    Commented Nov 2, 2022 at 20:43
  • \$\begingroup\$ @Pikalek rooms is a nested list (List<List<>>). What I'm trying to do create a sort of "Dumby" List<> and copy-paste it (so to speak) into the elements of rooms. Hence, first I clear row, then fill it, then add it, repeat. Weird thing is NS is completely unaffected by this. \$\endgroup\$ Commented Nov 3, 2022 at 2:29

1 Answer 1

1
\$\begingroup\$

Simple fix, the bug is not in lines 7 to 10 like I thought. Rather, because I'm adding the same instance of the List row, Clearing it, and adding it repeatedly, the rows get a little buggy. My fault for not seeing this.

Instead of rooms.Add(row), I should have been using rooms.Add(new List<Room>(row)). This way, when I dump row, I still have the data stored in rooms.

Special thanks to @DMGregory and @Pikalek

\$\endgroup\$
1
  • \$\begingroup\$ You may want to use .ToArray once you're done filling a row and know its final size, since you no longer need the dynamic resizing a list gives you. Or you could make the whole thing use a 2D array for better cache locality. \$\endgroup\$
    – DMGregory
    Commented Nov 3, 2022 at 10:33

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.