How do i get Neighboring Cells in Grid?

Hey guys i was able to come up with a grid that works the way i need it but i am having a hard time finding a good and easy way to get the connecting/neighboring cells to the selected cell.

below is a picture of how i construct the grid how would i go about getting the connected cells in blueprints?

What if each cell traced in 4 (6 for hexes) directions to check for a neighbour?

Here’s what I mean:

Image from Gyazo

To explain the 4 looped traces:

We don’t want to hit the very centre piece so I’m starting 55 uus from its middle and trace for an additional 50uus. Here the cubes are 100uus.

1 Like

Other than that, you may need to clarify what connected means here.

this could work in a sense and i really appreciate the clip and phot it does shine light on a way i could do this.

I guess what i mean by connected is i want each tile to know the tile next to it and be able to access its data i store in my map for those tiles.

Example

Below is a picture of a grid it uses the code i showed above. the first for loop is for the X tils the second is for the Y tiles so in this picture of the grid the high lighted tile is on tile X4Y4 Right now i have all this information saved into a Map of structs my only problem is i can go in and get the X and Y tile cords of the tile i line trace onto but the X and Y are values in the map not keys and i don’t know how to find what key is associated with that value in a map i cant seem to do it i can do a for each and get all the values of x and y but I cant seem to figure out how to filter all the X and Ys in the map values that are with in +1 or -1 of a given X or Y then once i have all those values of the tiles that are in that criteria how do i get the individual keys that would associate it to the grid cell. I’m sorry if i made it hard to understand as i am still learning how to use maps. maybe i would be better off making a struct full of arrays and accessing induvial arrays for whatever reason i need but i thought using maps and sets would make it cleaner and more centralized to keep all the cell data in one place.
So i think i need to either dive down the rabbithole of structs and arrays or i need to find out how to filter map keys by their values. but right now i know only how to filter map values by their keys and this is an isse because if i make the X and the Y a Key instead of a value it throws my whole project off and i would have to use the X and Y every time to access the map values but when i do a line trace it cant get the X and Ys because the grid is instanced it only used a single index and that’s how i identify each cell

Here is a picture of my grid map the key is the Cell Index and the value is everything else.
i thought using this method would be best because then i could have all cell data in one place and each cell could easily be accessed with a line trace from mouse to get the instancemesh index that would be the same for the grid cell as they are generated so using the index i can access all the data the cell has. i want to make a set of connected tiles to each cell so each cell would know the neighboring tiles and store that information. but as i said in previous comment idk how to filter keys with values i can only filter values by keys as of right now. idk if its even possible

I still can’t find out how to do what i want to do.

Below is how much grid is set u

e8986ff8b8101c92d1d9117fb84e39d7.gif

It works fine how it is and it uses Instance Meshes for the grid cells

what I am trying to do is when i select a grid cell as show i want to be able to access and know the 8 surrounding grid cells.

I cant seem to find a way to do it. my initial thought was find cells with in X-1 X+1 and Y-1 and Y+1
but every way i go about doing so i either can’t get the correct data as i am still learning arrays and maps.

The map for the instances used the Cords from the Grid Coord array that i use to generate the grid as the key and the value is the Instance it self. i also made a second map that is the direct opposite that uses the grid cell index as the key and the coords as the value. i just don’t know how to use the data to get the 8 neighboring grid cell indexes or 5 if on edge or 3 in corner. i know if i was using C++ i could just copy someone’s code and it could work but that doesn’t teach me anything and I’m using blueprints and would like to keep it in blueprint as this is how I’m currently learning.

Ideally for this test to show it works i would like to have the surrounding cells of the green square show a different color.

hope that clarifies my intent and thank you for your responses.

So you do not want to use the method I suggested? It would work with any arrangement, even if we had something odd shaped. 8 traces is not really that expensive. You could even just rotate the vector if you do not want to punch in directions.

Or do you need to mathematically calculate it for some other reason? Which is, of course, absololutely fair.

If you prefer to crack it yourself, draw the instance indexes to the screen - you’ll notice patterns they form.

Do note this method will not play nicely with HISMs as the indexing is more complicated due to culling and LODs. But should be fine for this.

I might be on to something at the moment. I was able to get an adjacent cell by subtracting the X size of the grid from the current hit index thus giving me the cell below it. I think if I make a function that calculates the 8 tiles based on grid size then add to a separate array or map then I could access on demand when i hit a given cell. similar to your approach but i do prefer it to be math based.

the game itself will be heavy so i would like to save all the resources i can. my only other thought other than I might get working right now is to make a map of adjacent cells of each cell on construction and store that data in each cell so once loaded its always there. I am not 100% sure the best way that will fill my need this is more or less a thing i set my self on to try and create myself so i can fully learn how grids and arrays and maps all work together. i really do appreciate your input though! and later tonight or tomorrow i will test your original method out and compare how they work and the resource difference.

For anyone wonder how to get neighboring cells in a grid when using a line trace to get a hit cell in blue prints I ended up making a function called “get possible neighbors” and it will take the grid cell hit and get all coords with in X-1,X+1,Y-1,Y+1 and then making a set out of all 8 possible neighboring cells i then take that set and cross reference it with the my main grid array to verify if the possible neighbor cells coords even exist and if they do i add them to an array called current neighbor cells that i then use on tick to always have all adjacent cells to the cell i get a line trace on.

Below is how it looked before and how it looks after along with the blueprint portion that does the math for getting the possible neighboring cells. hope it may help someone because even though i could find plenty of documentation on getting neighboring cells in c++ i couldn’t find a single thing in blueprint and that was frustrating me because i didn’t know how to use the data i stored in my maps and arrays correctly until i did a bunch of trial and error.

this is how it works on edges and corners