Premise:
I have this code that I’m using to save game data. The data being saved is a 2D array of chunks, in which each chunk contains 2 20x20 2D arrays, 2 for each tile in the chunk (for terrain data and object data on the tile). In the save file, first the map’s dimensions are printed, then for each chunk the terrain then object data are printed. Chunks are saved with whitespace between the two (so each chunk is saved as a 20x40 block of integers.
Problem:
However when I save it only saves the very last chunk. So if I have a 5x5 array of chunks then it only saves the one at index [4, 4]. If a 2x5 array then only at [1, 4]. It does save the chunks the “proper amount” of times however, so if 5x5 then chunk [4, 4] will be saved 25 times.
Code:
public void SaveGameData()
{
//Game Map Manager
MapManager manager = gameObject.GetComponent<MapManager>();
//Name of the Save File
string fileName = saveName + ".txt";
//Write Data to the Save File
StreamWriter writer = new StreamWriter(fileName, false);
//Get Data of each Chunk
int x = manager.chunkArray.GetLength(0);
int y = manager.chunkArray.GetLength(1);
//Get Number of Chunks
writer.WriteLine("Chunk Size: " + x + " " + y);
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
//Get the Chunk
GameObject saveChunk = manager.chunkArray[i, j];
Chunk chunkData = saveChunk.GetComponent<Chunk>();
int[,] terrainData = chunkData.terrainIDs;
int[,] objectData = chunkData.objectIDs;
//Iterate and Store the Terrain IDs
for (int k = 0; k < 20; k++)
{
//Break Between Sections
writer.WriteLine();
for (int l = 0; l < 20; l++)
{
//Store the Data
writer.Write(terrainData[k, l] + " ");
}
}
//Iterate and Store the Object IDs
for (int k = 0; k < 20; k++)
{
//Break Between Sections
writer.WriteLine();
for (int l = 0; l < 20; l++)
{
//Store the Data
writer.Write(objectData[k, l] + " ");
}
}
//Break between Chunks
writer.WriteLine();
}
}
writer.Close();
}