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:

C#

1

varcell=Grid[newPointyHexPoint(0,0)];

or

C#

1

varcell=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):

C#

1

2

3

4

foreach(varpoint inGrid)

{

varcell=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):

C#

1

2

3

4

overridepublicvoidOnClick(PointyHexPoint point)

{

varClickedCell=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:

C#

1

2

3

4

5

vargridPoint=Map[worldPoint];

if(Grid.Contains(gridPoint)

{

varcell=Grid[gridPoint];

}

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

C#

1

2

3

4

5

varpath=AStar(...);

foreach(varpoint inpath)

{

varcell=Grid[point];

}

You can also do you own filtering on grid points:

C#

1

2

3

4

5

varclosePoints=Grid.Select(p=>p.DistanceFrom(characterPosition)<4);

foreach(varpoint inclosePoints)

{

varcell=Grid[point];

}

And do calculations on points to get new points:

C#

1

2

varanotherPoint=somePoint+PointyHexPoint.NorthWest;

varcell=Grid[anotherPoint];

You can also find the neighbors of a point:

C#

1

2

3

4

5

varneighbors=Grid.GetNeighbors(somePoint);

foreach(varpoint inneighbors)

{

varcell=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:

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.