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?
rooms
? You're using a single instantiation forrow
.row
gets cleared at the top of the first loop & added torooms
at the bottom. Unlessrooms.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\$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 ofrooms
. Hence, first I clearrow
, then fill it, then add it, repeat. Weird thing is NS is completely unaffected by this. \$\endgroup\$