0

I have some voxel data I would like to save using BinaryWriter and then read using BinaryReader but I'm encountering some issues.

When I read it again, it seems like the data is in a different order and thus, my resulting voxel chunks get the wrong values. As I understood it you have to read the data in the order you wrote it, which I should be doing.
I've looked at several examples but and they all lead me to this issue. I don't know what I'm doing wrong.

I created some test code here to first write to a file and then read it right afterward and then check to see if the loaded values match the values it saveD. The result is always that it doesn't match and it always stops at the same place.

Block[] blocksToSave = chunk.blocks.GetBlocks();

using (BinaryWriter writer = new BinaryWriter(File.Open(Application.persistentDataPath + "/test.bin", FileMode.OpenOrCreate)))
{
    for (int i = 0; i < blocksToSave.Length; i++)
    {
        writer.Write(blocksToSave[i].id); // The ID is just a byte value.
    }
}

byte[] loadedBlocks = new byte[Chunk.CHUNK_SIZE * Chunk.CHUNK_SIZE * Chunk.CHUNK_SIZE]; // 16 * 16 * 16
using (BinaryReader reader = new BinaryReader(File.Open(Application.persistentDataPath + "/test.bin", FileMode.Open)))
{
    int pos = 0;
    int index = 0;
    int streamLength = (int)reader.BaseStream.Length;

    while (pos < streamLength)
    {
        byte id = reader.ReadByte();
        loadedBlocks[index] = id;
        pos += sizeof(int);
        index++;
    }
}

if (blocksToSave.Length != loadedBlocks.Length)
{
    Debug.LogError("Sizes does not match!");
    return;
}

for (int i = 0; i < blocksToSave.Length; i++)
{
    if (blocksToSave[i].id != loadedBlocks[i])
    {
        Debug.LogError("Expected " + blocksToSave[i].id + " but got " + loadedBlocks[i] + " at index " + i + ".");
        return;
    }
}

Any help to understand what the issue is greatly appreciated!
Thanks

4
  • 3
    sizeof(int) is 4 (bytes), but you're reading bytes - is that intentional? Commented Jan 24, 2020 at 21:38
  • 1
    Tested with sizeof(byte) and it works Commented Jan 24, 2020 at 21:39
  • 1
    @C.Evenhuis I'm so incredibly blind. I was working with ints before, changed it to bytes and then I guess I forgot I was working with bytes. Thanks for pointing it out. Commented Jan 24, 2020 at 22:12
  • 1
    @Hertzole happens to all of us :) Commented Jan 24, 2020 at 22:14

1 Answer 1

3

pos += sizeof(int);

should be

pos += sizeof(byte);

Sign up to request clarification or add additional context in comments.

1 Comment

Turns out I'm just blind. I had been using ints before, switched to bytes, forgot to change my saving and didn't think about that I was using bytes. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.