I have been looking to save a 2d-array from my terrain component to a file of some sorts. I have tried to save it to a .txt but for some reason, it did not finish? I even ran the code through a coroutine and it have me incorrect results, if im not mistaken…
My 2d-array is [296, 296] so that would be 296^2, which is 87616, but the text file only produced around 65574 lines (I had a counter value which was printed on the text file for debug reasons.). I even put the text file into mono-develop just to make sure it wasn’t a glitch with notepad but, it displayed the same amount of lines as my counter in the text file did. I got added some further debug values in the text and realised that my for loop, which I was using for the repetition, only seemed to go up to [255, 38] which I found odd.
Here is the code.
(I edited it a bit so that the for loop values are displayed on a separate line from the array value, but all you have to do is divide the last line’s number by 2…)
IEnumerator Write()
{
// Output Stream Writer variable
StreamWriter yourOSW;
// Open the file
yourOSW = new StreamWriter("C:/Users/" + Environment.UserName + "/Desktop/IslandData.txt");
// To write array to file
for(int i = 0; i < xRes; i++)
{
for(int j = 0; j < zRes; j++)
{
yourOSW.WriteLine(i.ToString() + " " + j.ToString() + " " + Environment.NewLine + heights[i,j]);
}
}
yield return null;
}
i & j both have the values of 296.