Need help populating objects in a grid.

Hello all!

I’ve just started scripting in C# and I am attempting to make a hex-tile strategy game. I’m hoping to get some advice on how to proceed.

To begin, I’d like to create a 10x10 grid through script that is populated with an object in each grid space (let’s say a cube for now).

I’m not terribly familiar with arrays, but my best guess is that I’d want to create a 2D array to get started. Something like:

GridSpace[,] gridArray = new GridSpace[10,10];

Can anyone point me in the right direction or suggest how I should proceed?

Thanks!

Hello,

Well I’d usually create a class to describe each tile on the board. Then use a 2D array as my board. You can then loop over the board (X and Y) to access the individual tiles…

Here’s a few little bits of (untested) code, but you’ll find loads of examples on the web.

// Tile class with any and all the properties you 
// need for your game..

public class Tile
{
  public int type;
  public bool status;

  public Tile(int type, bool status)
  {
    this.type = type;
    this.status = status;
  }
}

// Create a new array of 10x10 tiles like so..

Tile[,] board = new Tile[10, 10];

// Access them like so..

for (int x = 0; x < board.GetLength(0); x++)
{
      for (int y = 0; y < board.GetLength(1); y++)
      {
           Debug.Log(board[x,y].status);
      }
}

Good luck with the learning!

If you’re using a square grid to represent a hex grid, won’t that get a little weird? Squares touch 4 other squares, while hexes touch 6 other hexes. If you use a square grid to store the “behind the scenes” info, you’ll (probably) also have to keep an array that describes which “elements” touch which other “elements”.

Having six instead of four/eight adjacent tiles doesn’t mean you have to modify the underlying data structure of the map. The only things that change are the visual representation and movement/pathfinding/etc. (=the way the data is used), at it’s core a hexmap is nothing more than a 2d array.

Let’s start on well-treaded grounds: Getting the tile to the upper left on a square-tiled map is rather easy with a line like

upperLeft = tiles[currentTile.x-1][currentTile.y-1];

By comparison, hexgrids aren’t as intuitive to understand and use. Still, it’s possible to get the adjacent tiles with simple arithmetics.

As you can see in this shamelessly copypasted picture, every “odd” row is indented by half a column. This means that you can’t use a universally valid line like the one above to calculate adjacent tiles, you’d rather have to distinguish between even and odd rows:

if (isOdd) 	
	upperLeft = tiles[currentTile.x][currentTile.y-1];
else		
	upperLeft = tiles[currentTile.x-1][currentTile.y-1];

With a hexgrid like this all you need are 2x6 Vector2’s representing the relative coordinates of the adjacent tiles to move tanks, orcs and white mages across the board.

The hexgrid tutorial I took the picture from:
Isometric 'n' Hexagonal Maps Part I - General and Gameplay Programming - Tutorials - GameDev.net(it’s quite messy)

(Btw, I don’t speak C#)