- Home /
How to make a function that runs a loop x amount of times (Determined by parsed int)
So what I want is a function that checks for a certain radius but measures the radius off a Parsed Int. So if Rad is parsed as 2 it will get all the tiles within a radius of 1 and a radius of two. I am confused on how to do it at the moment
This is the code I'm using.
public List<CellCollection> GetRadius(Vector2Int Loc, int Rad)
{
var radius = new List<CellCollection>();
radius.Add(GetAt(Loc.x - 1, Loc.y - 1));
radius.Add(GetAt(Loc.x, Loc.y - 1));
radius.Add(GetAt(Loc.x + 1, Loc.y - 1));
radius.Add(GetAt(Loc.x - 1, Loc.y));
radius.Add(GetAt(Loc.x, Loc.y));
radius.Add(GetAt(Loc.x + 1, Loc.y));
radius.Add(GetAt(Loc.x - 1, Loc.y + 1));
radius.Add(GetAt(Loc.x, Loc.y + 1));
radius.Add(GetAt(Loc.x + 1, Loc.y + 1));
return radius;
}
This is the code I am using but I want it to check for each count in int Radius then add it to the List. CellCollection is a grid of tiles.
The way I think i can do this is through
public List<CellCollection> GetRadius(Vector2Int Loc, int Rad)
{
var radius = new List<CellCollection>();
radius.Add(GetAt(Loc.x - Rad, Loc.y - Rad));
radius.Add(GetAt(Loc.x, Loc.y - Rad));
radius.Add(GetAt(Loc.x + Rad, Loc.y - Rad));
radius.Add(GetAt(Loc.x - Rad, Loc.y));
radius.Add(GetAt(Loc.x, Loc.y));
radius.Add(GetAt(Loc.x + Rad, Loc.y));
radius.Add(GetAt(Loc.x - Rad, Loc.y + Rad));
radius.Add(GetAt(Loc.x, Loc.y + Rad));
radius.Add(GetAt(Loc.x + Rad, Loc.y + Rad));
return radius;
}
Thanks in advance.
Answer by Bunny83 · Dec 08, 2019 at 12:03 PM
Are you interested in the tiles in a certain radius (circular distance) or do you want to get a square of tiles around your location? Your code currently is generating a square and not a circle. The point Loc.x - 1, Loc.y - 1
does not have a radius of 1 but has a radius of 1.414.
Anyways in order to dynamically get as many tiles as you need you just need two nested for loops, one for x and one for y that looks like this:
for(int x = -Rad; x <= Rad; x++)
{
for(int y = -Rad; y <= Rad; y++)
{
radius.Add(GetAt(Loc.x + x, Loc.y + y));
}
}
This will get all tiles in the square around your location. The square has a side length of 2*Rad+1. So if Rad is 1 you get 9 tiles (3x3). If Rad is 2 you get 25 tiles (5x5) ...
However if you actually want to get a circle of tiles based on an actual radius you can do this:
public List<CellCollection> GetRadius(Vector2Int Loc, float Radius)
{
var results = new List<CellCollection>();
int Rad = Mathf.CeilToInt(Radius);
int r2 = Mathf.CeilToInt(Radius * Radius);
for(int x = -Rad; x <= Rad; x++)
{
for(int y = -Rad; y <= Rad; y++)
{
if (x*x+y*y < r2)
results.Add(GetAt(Loc.x + x, Loc.y + y));
}
}
return results;
}
This will still iterate through all tiles in the enclosing square of our circle but only add the tiles which are actually in the circle. Note that I made radius a float since whole numbers probably doesn't make much sense. This method will return only 5 tiles when you pass a radius of 1 (the center tile and the 4 cardinal directions) If you pass 1.5 as radius it would again be 9 tiles (3x3). If you want to use this approach you might have to try out some radii which give you the wanted results. Of course for small radii you barely recognise a circle. However the larget the radius the more it will look like an actual circle.