Generating grid graph given points as a list

I am trying to create a graph of nodes for my AI to walk around. The AI itself is fine but creating the node system is causing me trouble. Myabe Im going about it the wrong way.

Im trying to create a system where I can place the nodes in places, like corners etc and then each node knows the location of its neighbours so I have a class which is just an array of neighbours. But I dont want to go to each node, specify its size as well as drag and drop all its neighbours in. Too time consuming and I probably will make a mistake.

I drew a diagram as an example. The left is my nodes and the right is what id like, maybe shown with gizmos alt text

My nodes are all in a grid shape if that makes implementation easier i.e. same X and Z values for rows and columns. Is there some sort of algorithm to give each node its correct list of neighbours?

So far I have a master node class that has the list of nodes in an array and a list of the unique X and Z values but Im not sure what to do with all the info I have or what I would need to make this work.

Anyone have experience in this? Thanks.

Figured this out :stuck_out_tongue:

Added 2 more parameters to make it work though, the list of points should be column by column and I specify how many columns there are. This allows me to easily convert a 1d array into a 2d array.

Then I do a pass like this to add neighbours if allowed.

for(int i = 0; i < height; i++) {
	for(int j = 0; j < width; j++) {
	    if(i > 0) {
	        grid*[j].addNeighbours(grid[i-1][j]);*
  •  }*
    
  •  if(j > 0) {*
    

grid_[j].addNeighbours(grid*[j-1]);
}
if(i < height-1) {
grid[j].addNeighbours(grid[i+1][j]);
}
if(j < width-1) {
grid[j].addNeighbours(grid[j+1]);
}
}
}*

This also means the lines dont need to be in exact columns, so diagonals are allowed as long as I define it at the start correctly._