# Corrupt Save File Problem Csharp Opentk

I am trying to save player's objects but my loaded result is corrupt and placed in weird shapes like 3D arrows. Weirdly enough when terrain is saved, the terrain is fine but objects added later cause the problem. I am using OpenTK for OpenGL.

What I expect:

What I get:

The file seems to contain some repeated parts? Here is a part of the (large) file I end up with:

(0; 0; 0),(0; 0; 0),(0; 0; 1),(0; 1; 0),(1; 0; 0),(0; 0; 2),(0; 2; 0),(2; 0; 0),(0; 0; 0)


void MapLoad(string filelocation)
{
string[] map_;
map_ = line.Split(new char[] { ',', ';', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);

for (int xj = 0; xj < line.Split(',').Length; xj += 3)
{
// Here xj is an index of a X coordinate, xj + 1 is an index of a Y coordinate, xj + 2 is an index of a Z coordinate
//copied pasted for suring answer will work
block_file.Add(new Vector3(Int32.Parse(map_[xj]), Int32.Parse(map_[xj + 1]), Int32.Parse(map_[xj + 2])));
}

}


The Vector3 is from OpenTK.

Here is my writing code:

if(key.KeyCode == Keys.F7)
{
File.WriteAllText("usermap.txt", String.Join(",",block_file));//new char[] { ',', ';', '(', ')'
}


for (int xj = 0; xj < line.Split(',').Length - 2; xj++)


Inside the loop you are accessing xj, xj + 1 and xj + 2. Currently then your next iteration will use xj + 1, xj + 2 and xj + 3. You didn't include how you write the save file, but I assume there is a similar issue there as you seem to write vector components into wrong places.

Instead, you want to increment the loop counter with 3 each time, so that instead of reading the same values 3 times, you only read them once and then move onto the next element. So xj += 3 is what your loop increment should be.

for (int xj = 0; xj < map_.Length; xj += 3)
{
// Here xj is an index of a X coordinate, xj + 1 is an index of a Y coordinate, xj + 2 is an index of a Z coordinate
}


Also note that you want to iterate until map_.Length, since this is what contains your coordinate values, not line.Split(",").Length.

• how i can do that beacuse if i do thing you said it will read only small portion of file + Int32.Parse(map_[xj]) reads x coord of vector – ihatemyself Nov 5 '20 at 20:34
• No, with an increment of 3 it will read each vector written to the file exactly once. xj will refer to the X-component, xj + 1 will refer to the Y-component, xj + 2 will refer to the Z component. The next X-component is at index xj + 3, so your for loop should increment by 3. – Tyyppi_77 Nov 5 '20 at 20:47
• done thing you said but half of map is not rendered but its rendered half correclty now. probably fixing this i need put garble data in file too right? – ihatemyself Nov 5 '20 at 21:06
• Could you add your updated code to the question body? – Tyyppi_77 Nov 5 '20 at 21:16
• updated you can check it now – ihatemyself Nov 5 '20 at 21:20