- Home /
Delete
How would you store child Tags(colours) in an Array to detect if grid [x,y] && grid [x + 1, y] are the same colour?
Help please!
Been trying to work this out for a few days, I have 2 colours in a parent object that move down and fill up a grid. The colours move only in the grid and fills up fine but how do you go about detecting if 4 or more child colours are connected in the grid to make them disappear?
if child tag at [x, y] is black && child tag at [x + 1, y] is black { do something... }
Answer by Harinezumi · May 15, 2018 at 10:26 AM
It's a bit difficult to understand your question, but I'll try to help. For example, it is not clear if you only want to check in one direction (straight shapes), or they can have be connected in any way? A screenshot or drawing would also help a lot!
Anyway, the general way to go about this is to start from the recently arrived colour block, and recursively check each eligible neighbour if they are the same colour, adding them to a list if they are. Something like this:
// assuming you store colors as ints... see below why
private int[,] grid = new int[10, 10];
// returns in result the coordinates of all neighbors
private void GetConnectedColors (int x, int y, int colorValue, ref List<Vector2Int> result) {
// add checked cell to result, because we only check cells that have the desired color
result.Add(new Vector2Int(x, y));
// check immediate neighbors if they have the same color
List<Vector2Int> sameColorNeighbors = new List<Vector2Int>();
if (IsSameColor(x - 1, y, colorValue)) { sameColorNeighbors.Add(new Vector2Int(x - 1, y)); }
if (IsSameColor(x + 1, y, colorValue)) { sameColorNeighbors.Add(new Vector2Int(x + 1, y)); }
if (IsSameColor(x, y - 1, colorValue)) { sameColorNeighbors.Add(new Vector2Int(x, y - 1)); }
if (IsSameColor(x, y - 1, colorValue)) { sameColorNeighbors.Add(new Vector2Int(x, y + 1)); }
for (int i = 0; i < sameColorNeighbors.Count; ++i) {
Vector2Int coordinate = sameColorNeighbors[i];
// check same color neighbors if they are new
if (!result.Contains(coordinate)) {
// check the immediate neighbors' neighbors, and theirs... recursively!
GetConnectedColors(coordinate.x, coordinate.y, colorValue, ref result);
}
}
// result is automatically "returned", because it is marked as reference
}
private bool IsSameColor (int x, int y, int colorValue) {
return x < grid.GetLength(0) && y < grid.GetLength(1) && grid[x, y] == colorValue;
}
To use this, create a List<Vector2Int>
and pass it to GetConnectedColors()
using the ref
keyword, and the coordinates of the grid cell you want to check. It will recursively check each neighbour that is not yet in result.
Also note that I used int[,]
for the type of grid
, because you shouldn't compare variables of Color
, because they store rgb values as float
, which you cannot compare precisely. You also cannot compare Color32
(although you can easily write one for it:
// by default it also compares alpha, pass true as third parameter to ignore alpha difference
public static bool Color32Equal (Color32 lhs, Color32 rhs, bool ignoreAlpha = false) {
return lhs.r == rhs.r && lhs.g == rhs.g && lhs.b == rhs.b && (ignoreAlpha || (lhs.a == rhs.a));
}
Hope this helps!
Follow this Question
Related Questions
Enable Disable Components attached to tagged Game Objects 1 Answer
Need help with setting all items of a specific tag to "inactive" when i die. 0 Answers
How to return the position of an object in a 2 dimensional array 1 Answer
How to make multiple objects share same variable 1 Answer
Need help with setting all items of a specific tag to "inactive" when i die. 1 Answer