You’d think that NetworkWriter.Write(byte[ ] buffer, int offset, int count) would append a slice of the buffer to the NetworkWriter, from ‘offset’ to ‘offset+count’ within the buffer.
The docs (Unity - Scripting API: Networking.NetworkWriter.Write ) seem to imply the same, saying offset is “The byte buffer array element to start writing from.”
However, this is actually very misleading (or plain wrong, depending on your interpretation), as what actually happens is that offset is the array element to start writing TO, in the NetworkWriter’s internal buffer! See here:
https://bitbucket.org/Unity-Technologies/networking/src/dafe85bfafa5c84a6dfad0126c5f99eda45e1d9a/Runtime/NetworkWriter.cs?at=2018.3&fileviewer=file-view-default#NetworkWriter.cs-323
…which calls through to this:
https://bitbucket.org/Unity-Technologies/networking/src/dafe85bfafa5c84a6dfad0126c5f99eda45e1d9a/Runtime/NetworkBuffer.cs?at=2018.3&fileviewer=file-view-default#NetworkBuffer.cs-99
So - while this is very useful to have, if for some reason you need random-access writing capability within a NetworkWriter, it’s pretty much exactly the wrong thing you want from an overloaded Write method where every other overload appends to the end of the stream. Thankfully I only wasted 1 day working this out for myself. Hope I can save someone else a little time in future…
(Related: [Resolved] Unity 5.2 NetworkWriter.Write(byte[] buffer, int offset, int count) uses offset in 5.2 - where apparently the offset argument wasn’t even used until 5.2)
TwoTen
April 4, 2019, 11:09am
2
Kylotan_1:
You’d think that NetworkWriter.Write(byte[ ] buffer, int offset, int count) would append a slice of the buffer to the NetworkWriter, from ‘offset’ to ‘offset+count’ within the buffer.
The docs (https://docs.unity3d.com/2017.4/Documentation/ScriptReference/Networking.NetworkWriter.Write.html ) seem to imply the same, saying offset is “The byte buffer array element to start writing from.”
However, this is actually very misleading (or plain wrong, depending on your interpretation), as what actually happens is that offset is the array element to start writing TO, in the NetworkWriter’s internal buffer! See here:
https://bitbucket.org/Unity-Technologies/networking/src/dafe85bfafa5c84a6dfad0126c5f99eda45e1d9a/Runtime/NetworkWriter.cs?at=2018.3&fileviewer=file-view-default#NetworkWriter.cs-323
…which calls through to this:
https://bitbucket.org/Unity-Technologies/networking/src/dafe85bfafa5c84a6dfad0126c5f99eda45e1d9a/Runtime/NetworkBuffer.cs?at=2018.3&fileviewer=file-view-default#NetworkBuffer.cs-99
So - while this is very useful to have, if for some reason you need random-access writing capability within a NetworkWriter, it’s pretty much exactly the wrong thing you want from an overloaded Write method where every other overload appends to the end of the stream. Thankfully I only wasted 1 day working this out for myself. Hope I can save someone else a little time in future…
(Related: https://discussions.unity.com/t/598146/13 - where apparently the offset argument wasn’t even used until 5.2)
UNET is being deprecated anyways. It probably won’t be fixed, but you can still file a bug report to Unity.
1 Like
I know it’s being deprecated, but it’s still being used by a lot of people, so I just wanted to note it here.