0
\$\begingroup\$

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: expected result

What I get: Result That Happens

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)

Here is my loading code:

void MapLoad(string filelocation)
{
    string[] map_;
    string line = File.ReadAllText(filelocation);//
    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[] { ',', ';', '(', ')' 
}
\$\endgroup\$
0
\$\begingroup\$

Your indexing is broken.

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.

\$\endgroup\$
7
  • \$\begingroup\$ 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 \$\endgroup\$ – ihatemyself Nov 5 '20 at 20:34
  • \$\begingroup\$ 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. \$\endgroup\$ – Tyyppi_77 Nov 5 '20 at 20:47
  • \$\begingroup\$ 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? \$\endgroup\$ – ihatemyself Nov 5 '20 at 21:06
  • \$\begingroup\$ Could you add your updated code to the question body? \$\endgroup\$ – Tyyppi_77 Nov 5 '20 at 21:16
  • \$\begingroup\$ updated you can check it now \$\endgroup\$ – ihatemyself Nov 5 '20 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.