How do I reference either the x or y field of a Vector2 depending on some condition?

Hello. I'm resizing a grid, to do so I use two functions, the first resizes the grid in the X axis, the second on the Y axis. I would like to merge them but I have difficulties coming up with a solution. My main issue is that I would need to have some way to tell the method to either use (and update, very important) the x or y field of a Vector2 depending on some boolean (if changeXAxis = true then use coordinates.x else use coordinates.y, if it makes sense). I have tried multiple things, for example setting variables with a ternary operator like so:
var (directionChange, coordinateX, coordinateY, targetSizeX, currentSizeY)
= modifyAxisX
? (directionChangeX, coordinates.x, coordinates.y, targetSize.x, currentSize.y)
: (directionChangeY, coordinates.y, coordinates.x, targetSize.y, currentSize.x);
var (directionChange, coordinateX, coordinateY, targetSizeX, currentSizeY)
= modifyAxisX
? (directionChangeX, coordinates.x, coordinates.y, targetSize.x, currentSize.y)
: (directionChangeY, coordinates.y, coordinates.x, targetSize.y, currentSize.x);
But then coordinateX become it's own variable, I have tried using the ref keyword in different ways but couldn't get it to make this work. Is there some concept I'm not aware of that would solve my issue or am I stuck with two methods that do basically the same thing? On a side note, I know I should put the ModifyGrid() outside of the UpdateTiles() one but this is very much a work in progress still. I'm putting my current method below because it's just slightly too big to comfortably fit in an image. The other is the same, just inverted basically.
void ModifyGridX()
{
for (coordinates.y = 0; coordinates.y < currentSize.y; coordinates.y++)
{
coordinates.x = currentSize.x - 1;
while (coordinates.x + 1 != targetSize.x)
{
if (directionChangeX == -1)
{
DestroyTile(coordinates);
coordinates.x += directionChangeX;
}
else
{
coordinates.x += directionChangeX;
CreateTile(coordinates);
}
}
}
currentSize.x = targetSize.x;
}
void ModifyGridX()
{
for (coordinates.y = 0; coordinates.y < currentSize.y; coordinates.y++)
{
coordinates.x = currentSize.x - 1;
while (coordinates.x + 1 != targetSize.x)
{
if (directionChangeX == -1)
{
DestroyTile(coordinates);
coordinates.x += directionChangeX;
}
else
{
coordinates.x += directionChangeX;
CreateTile(coordinates);
}
}
}
currentSize.x = targetSize.x;
}
1 Reply
coffeeisntforkitties♡
I think the issue you are running into is that Vector2 is a type of struct which is a value type and you are trying to use it like a reference type.
Want results from more Discord servers?
Add your server