Accessing Points and Cells

It’s useful to think of a Grid as a thing such as an array or dictionary, that is accessed through indices or keys. The keys, in the case of a grid, is a point, such as PointyHexPoint. Cells are then similar to the elements of an array or the values in a Dictionary.

From a GridBehvaiour, you can access the cell at the origin (0, 0) like this:

var cell = Grid[new PointyHexPoint(0, 0)];


var cell = Grid[PointyHexPoint.Zero];

So this is the most direct way of accessing cells, but probably the least useful.

For accessing all the cells in the grid, one by one, you can use the following idiom (this is all in the GridBehaviour you need to implement):

foreach(var point in Grid)
   var cell = Grid[point];

There is also a OnClick (and OnLeftClick and OnRightClick) method in GridBehaviour that you can override to handle when the player clicks on a cell (this is for 2D only):

override public void OnClick(PointyHexPoint point)
   var ClickedCell = Grid[point];

Then, if you have a world coordinate  (Vector3) and want to know what is the associated grid point, you can use the map, like this:

var gridPoint = Map[worldPoint];
   var cell = Grid[gridPoint];

Many algorithms produce points that you can use to access the grid.

var path = AStar(...);
foreach(var point in path)
   var cell = Grid[point];

You can also do you own filtering on grid points:

var closePoints = Grid.Select(p => p.DistanceFrom(characterPosition) < 4);
foreach(var point in closePoints)
   var cell = Grid[point];

And do calculations on points to get new points:

var anotherPoint = somePoint + PointyHexPoint.NorthWest;
var cell = Grid[anotherPoint];

You can also find the neighbors of a point:

var neighbors = Grid.GetNeighbors(somePoint);
foreach(var point in neighbors)
   var cell = Grid[point];

Those are the basic ways you get hold of cells. The basic idea is, you get hold of them through points. It’s important to realize that most of you logic that has to do with position / geometry on the grid will be in terms of grid points.

One important thing

The cells you will get as explained above are of type TileCell. You will need to cast them to your specific cell.

It is often useful to make a copy of the grid that will give cells of your own type:

PointyHexGrid<MyCell> myGrid = (PointyHexGrid<MyCell>) Grid.CastValues<MyCell>();
//Just to show which types you get from the different grids
MyCell myCell = myGrid[somePoint];
TileCell cell = Grid[somePoint];

Why can I not access points from cells?

For the same reason the elements of an array don’t know their array positions, or the values in a dictionary don’t know their keys, cells in a grid do not know their positions in the grids (their grid points). This generally means your algorithms should be designed on grid level, rather than cell level, which makes code a lot cleaner, especially when you need to look at other cells.

But I really need the point information in my cell

In rare cases, you really do need the point information in your cell. If this is the case for you, you need to design your own cells to maintain this data. You need to be careful if you move cells around in the grid to keep the information up to date, and you will need to consider how to deal with cells that are not in the grid.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top