I’m making a platforming game that uses arrays to store the 32x32 tile playfield. There are three arrays that are used in this process. When I construct a Level class object, a specific array is put in that constructor.
final int[][] layout00 = { ... };
...
levels[0] = new Level(layout00);
Notice how the array I used was Final. It will become important later.
Within the constructor itself, the array is copied to a new array, layout[][], which is local to each instance of the object.
Level(int tempLayout[][]) {
arrayCopy(tempLayout, layout);
}
The third array in the sequence is stage[][]. On a level loading in, it copies it’s layout[][] to the stage[][] and layout[][] is never mentioned anywhere else in the code.
void load() {
arrayCopy(layout, stage);
}
...
void resetLevel() {
levels[currentLevel].load();
}
The idea is that the player can change tiles in the level, but not actually change anything in the level’s layout[][]. This is so when the player has made a mistake, they can press a key to reset the level to it’s initial state. I do this by reloading the level with load(), again.
For some odd reason, the level doesn’t change at all. All the changes the player made to the stage[][] are still there. After some debugging, I found that somehow, the levels layout[][] was being edited as stage[][] was being edited. This should be impossible because, after some further searching, there is no code outside of the Level class that references the level’s layout[][].
Some more debugging lead me to find the final array, layout00[][], was being edited as well. How is this possible? There is no code in my project that even references layout00[][]. And I’m not even getting any error messages that it is unable to be edited!