So I am working on a mini game that dynamically creates quads of 2 * 2 or 2 * 4 or 4 * 4. The player is then prompted to tap on a random neighbouring quad starting from the bottom right. The neighbouring quad needs to be chess move king distance from the current grid point. New neighbouring grid points are prompted through out the game. How could I write code that finds neighbouring quads from a given current quad point?
This is the current code creating grid points :
public class GenerateQuads : MonoBehaviour
{
[SerializeField]
private StepCountQuads stepCountQuads;
[SerializeField]
private int quadColumns;
[SerializeField]
private int quadRows;
[SerializeField]
private int quadColumnOffSet;
[SerializeField]
private int quadRowOffSet;
private Vector3[,] gridPoints;
private int[,] currentGridPoint;
private int[,] nextGridPoint;
private int totalQuads;
public void CreateQuadArray()
{
totalQuads = quadRows * quadColumns;
gridPoints = new Vector3[quadRows, quadColumns];
currentGridPoint = new int[quadRows, quadColumns];
for (int row = 0; row < gridPoints.GetLength(0); row++)
{
gridPoints[row, 0] = new Vector3(quadRowOffSet * row,0,0);
for (int col = 0; col < gridPoints.GetLength(1); col++)
{
gridPoints[row, col] = new Vector3(quadRowOffSet * row, 0, quadColumnOffSet * col);
MakeQuad(gridPoints[row, col]);
}
}
}
public void MakeQuad(Vector3 quadPosition)
{
var Quad = Instantiate(Resources.Load("Prefabs/Quad"), quadPosition, Quaternion.Euler(90,0,0)) as GameObject;
var quadIndex = UnityEngine.Random.Range(0, totalQuads);
Quad.name = stepCountQuads.quads[quadIndex].QuadTitle.ToString();
Quad.GetComponent<QuadController>().QuadID = stepCountQuads.quads[quadIndex].QuadId;
Quad.GetComponent<QuadController>().QuadMaterial = stepCountQuads.quads[quadIndex].QuadMaterial;
}
}
The first pinned post in the forum asks you to use code tags . So please edit your post to include them.
And parts of your code is missing (especially some variable declarations).
When you write “dynamically creates quads” do you refer to (A) while the player is playing the game or (B) before the player plays?
Usually to refer to neighbouring cells of your grid you would need a data structure to refer to them which could be setup during initialization for scenario (A). For scenario (B) you would need some kind of “coordinate” which can be found. So when you are at 5,2 and want the cell to the right you query a manager class which knows each cell for 6,2.
“dynamically create quads” this happens at the beginning of the game. I can know each cell but how do I then find the neighbouring cells from the current cell. So if current cell is 2,4 how can I fins all the surrounding neighbours from that cell? A manager class with a list of neighbouring cell would not work as these grid are dynamic and can be different at any time.
You have 2 for loops which create the grid. You can create the necessary information right there. The only issue is, that parts of the grid are not instantiated yet so when you go from left to right you can’t assign the right neighbour now as it does not yet exist.
Some methods to mitigate this:
Assign the instantiated GameObjects a name with the position. You can then look for them with GameObject.Find (not recommended for frequent use in Update, and strings have some flaws/issues for this usecase).
Have a manager class with an 2 dimensional array of GameObject references which is filled (Instantiate returns the instance).
Have a coordinate struct which is used as index/key in a Dictionary and the value is the Quad/GameObject at that point. Not that “coordinate” refers to grid coordinates not Unity world coodinates.
When dynamic refers to start of the game/level this is no issue at all when you do it right. That’s what a manager is for (in programming context). Your GenerateQuads class is practically a manager class. Now it only manages the creation but with a bit additonal information it could also manage access to certain grid cells / quads.
You have the “neighbouring” information at hand when you create the grid. You just have to make it “persistant” in a certain way to utilize it for your desires.