- Home /
I guess I solved this, somehow.
Changing One Value in an Array Changes All Values
I've created a 2D array of object called "tile", who's member variables are
GameObject entity; int t_top; int t_right; int t_bottom; int t_left;
Whenever I assign a tile value to any element in the 2D array, every element in the array is changed to that same value. I tried to only include the most relevant snippets of code:
T[,] InitializeArray<T>(int height, int width) where T : new()
{
T[,] array = new T[height, width];
for (int i = 0; i < height; ++i)
{
for (int g = 0; g < width; ++g)
{
array[i, g] = new T();
}
}
return array;
}
...
public void initialize_straight ()
{
entity = fabStraight_Tile;
t_top = 1;
t_right = -1;
t_bottom = 1;
t_left = -1;
}
public void initialize_blank ()
{
entity = fabBlank_Tile;
t_top = -1;
t_right = -1;
t_bottom = -1;
t_left = -1;
}
public GameObject get_entity ()
{
return entity;
}
...
const int BUFFER = 2;
const int GRID_HEIGHT = 2 + BUFFER;
const int GRID_WIDTH = 2 + BUFFER;
int heightCounter;
int widthCounter;
...
tile[,] tilesAry = InitializeArray<tile>(GRID_HEIGHT, GRID_WIDTH);
...
for (heightCounter = 0; heightCounter < GRID_HEIGHT; heightCounter++)
{
for (widthCounter = 0; widthCounter < GRID_WIDTH; widthCounter++)
{
tilesAry[heightCounter, widthCounter].initialize_blank();
}
}
for (heightCounter = 0; heightCounter < GRID_HEIGHT; heightCounter++)
{
for (widthCounter = 0; widthCounter < GRID_WIDTH; widthCounter++)
{
print (tilesAry [heightCounter, widthCounter].get_entity () + " yyy");
}
}
print (" ");
for (heightCounter = 0; heightCounter < GRID_HEIGHT; heightCounter++)
{
for (widthCounter = 0; widthCounter < GRID_WIDTH; widthCounter++)
{
tilesAry [heightCounter, widthCounter].initialize_straight();
}
}
print (" ");
for (heightCounter = 0; heightCounter < GRID_HEIGHT; heightCounter++)
{
for (widthCounter = 0; widthCounter < GRID_WIDTH; widthCounter++)
{
print (tilesAry [heightCounter, widthCounter].get_entity () + "yyy");
}
}
In the above snippet, the first time it prints out saying all of the tiles are blank (as it should), while the second time it says that all are straight (when only one of them should be.
However, for whatever reason, this second array (DUBB) that I made just for testing this problem works normally, and does not show any sign of this same issue:
tile[,] DUBB = new tile [3, 3];
DUBB[0,0] = new tile ();
DUBB[1,1] = new tile ();
DUBB[2,2] = new tile ();
DUBB[0,0].initialize_blank ();
DUBB[1,1].initialize_blank ();
DUBB[2,2].initialize_blank ();
print (DUBB[0,0].get_entity ());
print (DUBB[1,1].get_entity ());
print (DUBB[2,2].get_entity ());
print ("Break");
DUBB[1,1].initialize_straight();
print (DUBB[0,0].get_entity ());
print (DUBB[1,1].get_entity ());
print (DUBB[2,2].get_entity ());
Is there any other code that I might need to add to better display what is happening?
I'm not entirely sure what you are saying. You are doing two completely different things in your last two blocks. The first one, with the nested for loop, is going to every single place in the array and setting the value to nullTile. In the second one, you are accessing specific indices manually, and calling a method.
If you want the top version to be like the bottom version, then you need to add a bit of code to check the value of height counter vs that of width counter, and only assign the value if they are equal.
Provide initialize_null() , assign() functions. We can't know what goes in there. If "initialize_null()" uses shared variable as null, which I suspect you are doing, "tilesAry[heightCounter, widthCounter] = nullTile;", you would end up with same references over the array.
Now, if assign is something like "tile.x = inputTile.x" kind of value copying, then the last block will set "nullTile" to whatever "straightTile" is.
If you want such manner, I suggest you change to struct.
You haven't given us enough information. Is DUBB the second array that works? When do you assign values to the first array that isn't working? What does initialize_null() do?
while the second time it says that all are straight (when only one of them should be)
I may have missed something, but as far as I can tell, your code is iterating over every element and setting it as straight, so I would expect every element to become straight.
for (heightCounter = 0; heightCounter < GRID_HEIGHT; heightCounter++)
{
for (widthCounter = 0; widthCounter < GRID_WIDTH; widthCounter++)
{
// initialize_straight() is being called for every single element.
tilesAry [heightCounter, widthCounter].initialize_straight();
}
}
Thanks; that wasn't the problem, but it led me to finding it, even though I still don't entirely understand it.