Now my question:
If you check the Reading Side you’ll see that I wrote “r.ReadBytesSafe(ref bytes, r.Length - 8);”
I did this because for some reason the FastBufferReader adds an additonal length of 8 to the actual string defined in the writing side.
Respectively when I send data with 100 characters, the reading side will have 108. If I do not remove those addtional 8 I will get an Overflow Exception. If I remove them then it works as it should.
Is there a reason behind that behaviour or just buggy or am I doing something completely wrong? .
Thanks for your replies! If something is unclear, let me know. Thank you!
When you write something variable size like a string you can’t rely on the length of the FastBufferReader because Netcode for GameObjects writes other meta information into that reader. Instead first write the length of the string into the writer and then on the receiving side read the length and after that read that many bytes.
What if you don’t know beforehand how long the string is going to be? For example sending some information only the server knows, but clients need to get informed about that.
On such a case is it better to define a fixed length for the buffer?
Would be great to update the documentation on this, as no where it explains how FastBufferReader and Writer are structured, I was just trying to copy a FastBufferReader into a FastBufferWriter and it’s really unclear how to do this.
This code seems to work:
reader.Seek(0); //Reset reader
reader.ReadValueSafe(out ulong hash); //Read header
byte[] bytes = new byte[reader.Length - 8]; //Header is ulong (8 bytes)
reader.ReadBytesSafe(ref bytes, reader.Length - 8); //Header is ulong (8 bytes)
FastBufferWriter writer = new FastBufferWriter(bytes.Length, Allocator.Temp);
writer.WriteBytesSafe(bytes, bytes.Length);
But this doesn’t, even though in theory it’s supposed to be the same
reader.Seek(0); //Reset reader
byte[] bytes = new byte[reader.Length - 8]; //Header is ulong (8 bytes)
reader.ReadBytesSafe(ref bytes, reader.Length - 8, 8); //Header is ulong (8 bytes), offset is also 8 to skip header
FastBufferWriter writer = new FastBufferWriter(bytes.Length, Allocator.Temp);
writer.WriteBytesSafe(bytes, bytes.Length);
So there is something unclear about how these work. Would be really good to have some doc on this.
I made a free asset to help get started with Netcode. Many people told me this asset really helped them because i created a lot of premade functions and examples to help with stuff that aren’t clear.
For these types of things you need to use Game Services, not multiplayer frameworks. NGO is a multiplayer framework. Unity Game Services are what you’re looking for