com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

FastBufferReader does not respect ArraySegment.Offset

Open Risord opened this issue 1 year ago • 1 comments

Description

var bytes = new byte[] { 0, 1, 2, 3 };
var segment = new ArraySegment<byte>(bytes, 1, 3);
var reader = new Unity.Netcode.FastBufferReader(segment, Unity.Collections.Allocator.Temp);
Debug.Log(string.Join(',', segment.ToArray())); // => 1,2,3
Debug.Log(string.Join(',', reader.ToArray())); // => 0,1,2

Expected Outcome

Both prints output: 1,2,3

Environment

  • OS: Windows 11
  • Unity Version: 2022.3.24
  • Netcode Version: 1.8.1

Risord avatar Apr 17 '24 15:04 Risord

@Risord Yeah, this indeed does look like it does not honor the ArraySegment and a fix would be for us to create a new constructor that does not include the length or offset parameters in order to be 100% sure of the intended usage.

For now, the work around would be to add the offset in the constuctor:

            var bytes = new byte[] { 0, 1, 2, 3 };
            var segment = new System.ArraySegment<byte>(bytes, 1, 3);
            var reader = new Unity.Netcode.FastBufferReader(segment, Unity.Collections.Allocator.Temp, offset: 1);

NoelStephensUnity avatar Apr 22 '24 14:04 NoelStephensUnity