Search Unity

OnSerialize / OnDeserialize help, IndexOutOfRangeException: NetworkReader:ReadByte

Discussion in 'Multiplayer' started by marcV2g, Aug 10, 2017.

  1. marcV2g

    marcV2g

    Joined:
    Jan 11, 2016
    Posts:
    115
    public override bool OnSerialize(NetworkWriter writer, bool initialState)
    {
    if (this.syncVarDirtyBits == 0 && initialState == false)
    {
    return false;
    }
    else
    {
    writer.Write(this.transform.position);
    return true;
    }
    }
    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
    realPos = reader.ReadVector3();
    dist = Vector3.Distance(this.transform.position, realPos);
    }

    results in:

    IndexOutOfRangeException: NetworkReader:ReadByte out of range:NetBuf sz:16 pos:16

    Any idea why?
     
  2. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    dunno what you doing, but first, remove this:
    if (this.syncVarDirtyBits == 0 && initialState == false)
    {
    return false;
    }

    the packet is never sent if the dirty is 0, and I think you shouldn't ever return false on it?
     
  3. marcV2g

    marcV2g

    Joined:
    Jan 11, 2016
    Posts:
    115
    I though the docs said returning false means do not send anything.
     
  4. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    I see, it seems it does set the dirtybits to 0.

    Are you sure you don't have any syncvar variables?
     
  5. marcV2g

    marcV2g

    Joined:
    Jan 11, 2016
    Posts:
    115
    So it seems like returning false does send a packet with no data and no way to detect that it is useless.

    I am still on 5.6.2, does anyone know if this is a fixed bug, open bug or intended behaviour.
     
  6. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    Wait, do you have more than one script that inherits from networkbehaviour on that object?

    Because it is a known bug that if any of them send a packet, all of them will send one, even if it will be empty.
     
  7. marcV2g

    marcV2g

    Joined:
    Jan 11, 2016
    Posts:
    115
    I am inheriting from a class that inherits network behavior but the middle class does not overload Serialize/Deserialize
     
  8. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    uh, that wasn't what I asked
     
  9. marcV2g

    marcV2g

    Joined:
    Jan 11, 2016
    Posts:
    115
    Ok, now I understand what you meant.
    Yes there were multiple network behaviours on the game object so that is the cause.
    Is this one of the never going to be fixed bugs I just need to work around?
     
  10. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    Yes, I think the workaround is either sending your own dirtypacket or never returning false on it.
     
  11. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347