How to return the position of an object in a 2 dimensional array

Hi all.

So, I’m trying to create a functional hex grid for a game. I’ve successfully managed to create a script which creates the grid itself in the editor (not when the project runs) with instantiate, storing each hex object in a 2 dimensional array like so:

hex[columnNumber,j] = (GameObject)Instantiate(hexObject, new Vector3((j * offsetRow) + rowAdjust, gridHeight, ((float)columnNumber * offsetColumn)), Quaternion.Euler(90, 0, 0));

The position of each hex in that array relates to its “address”; i.e. hex(0,0) is at the top left, the one below it is hex(0,1), the hex above and to the right of that is hex(1,1), and so on.

Now comes the tricky bit. When a hex is selected at runtime (i.e. using OnMouseDown), I want to be able to assign and pass to each hex object an integer value for how far away those other hexes are from the selected hex; i.e. the hexes directly joining the selected hex would have a value of 1, those one step on a value of 2, and so on. This should also be able to deal with missing/impassable hexes (e.g. hex(2,3) might not exist, which might effect what distance value a given hex gets).

My idea to achieve this (and I’m prepared to be told that there are better ways) is to check an object’s position in the array (i.e the address of the hex) and then pass that address to some logic.

So:

  • Will the object retain its position in the array at runtime (given that I’ve placed it in an array in a editor script), or will that array disappear once the editor script finishes?
  • If so how can I find a given hex’s position in each dimension of the array?
  • And then how can I assign a value to each hex in the array denoting its distance?
  • If not then how do I assign the address values to the object when I create it?

This is a learning project, so elegant answers are appreciated as much a functional ones.

Thanks in advance for your help.

P.S. For the sake of completeness, here is functional part of the script that creates the hexes:

 public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(createHexGrid), true, "Hex Grid Creator");
    }

    void OnGUI()
    {
        GUILayout.Label("Base Hex Unit", EditorStyles.boldLabel);
        hexObject = EditorGUILayout.ObjectField(hexObject, typeof(Object), true);

        GUILayout.Label("Grid Settings", EditorStyles.boldLabel);
        numberOfRows = EditorGUILayout.IntField("Number of Rows:", numberOfRows);
        numberOfColumns = EditorGUILayout.IntField("Number of Columns:", numberOfColumns);
        topConvex = EditorGUILayout.Toggle ("Top Convex", topConvex);
        bottomConvex = EditorGUILayout.Toggle("Bottom Convex", bottomConvex);

        GUILayout.Label("Offsets", EditorStyles.boldLabel);
        gridHeight = EditorGUILayout.FloatField("Grid Height", gridHeight);


        if (GUILayout.Button("Create Grid"))
        {
            GameObject gridParent = new GameObject();
            gridParent.name = "Grid";

            hexesInOddColumns = numberOfRows - 1 + (topConvex ? 1 : 0) + (bottomConvex ? 1 : 0);


            for (int i = 0; i < (numberOfColumns / 2); i++)
            {
                hexColumnCreation(numberOfRows, 2 * i, 0);
                hexColumnCreation(hexesInOddColumns, (2 * i) + 1, ((offsetRow / 2) - (topConvex ? offsetRow : 0)));
            }
            if (numberOfColumns % 2 != 0)
            {
                hexColumnCreation(numberOfRows, numberOfColumns - 1, 0);
            }
        }
    }

    //--FUNCTION TO CREATE HEXES--//

    void hexColumnCreation(int hexesInColumn, int columnNumber, float rowAdjust)
    {
        for (int j = 0; j < hexesInColumn; j++)
        {
            hex[columnNumber,j] = (GameObject)Instantiate(hexObject, new Vector3((j * offsetRow) + rowAdjust, gridHeight, ((float)columnNumber * offsetColumn)), Quaternion.Euler(90, 0, 0));

            hex[columnNumber, j].name = string.Format("Hex({0},{1})", columnNumber, j + ((topConvex ? 0 : 1) * (columnNumber % 2)));

        }

    }

}

Will the object retain its position in the array at runtime (given that I’ve placed it in an array in a editor script), or will that array disappear once the editor script finishes?

I see no reason why the array would “disappear” during runtime.

If so how can I find a given hex’s position in each dimension of the array?

You could try implementing a custom pathfinding solution, or just do it like this:
“Walk” through the hex coordinates from the hex1s x position towards the hex2s x position by increasing or decreasing the x value by 1 each step until hex2s x value is reached. then do the same for y. Check each coordinate on the way if it actually contains a hex, and if not, offset the x or y values by one, then go on but remember to reverse the offset again next step if it’s possible, and so on. It should work in theory, at least :slight_smile:

    And then how can I assign a value to each hex in the array denoting its distance?

See above, OR just take add the differences between the x and y values if no pseudo-pathfinding is needed.

If not then how do I assign the address values to the object when I create it?

I don’t know what you mean.