Search Unity

Custom INetworkSerializable type not deserializing correctly?

Discussion in 'Netcode for GameObjects' started by bajabes2, Aug 2, 2021.

  1. bajabes2

    bajabes2

    Joined:
    Jul 25, 2018
    Posts:
    3
    I have this class:

    Code (CSharp):
    1. public class FixedUpdateInputEventArgs : EventArgs, INetworkSerializable
    2. {
    3.     public Vector2 lookInput;
    4.     public Vector2 moveInput;
    5.     public float mouseSensitivity;
    6.  
    7.     public void NetworkSerialize(NetworkSerializer serializer)
    8.     {
    9.         serializer.Serialize(ref lookInput);
    10.         serializer.Serialize(ref moveInput);
    11.         serializer.Serialize(ref mouseSensitivity);
    12.     }
    13. }
    and I have this ServerRpc:

    Code (CSharp):
    1. [ServerRpc]
    2.     void SendInputServerRpc(FixedUpdateInputEventArgs clientInputEventArgs){
    3.         //...some debug code above this
    4.         if(clientInputEventArgs.moveInput.magnitude > 0)
    5.         {
    6.             //TODO: Client is definitely sending non-zero mouse input but we're getting only (0,0) on the server side for some reason
    7.             Debug.Log("Client moved mouse");
    8.         }
    9.         //...some more code below this
    10.     }
    Here is where the client creates one of these objects:

    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         if (!IsLocalPlayer){
    4.             return;
    5.         }
    6.  
    7.         Vector2 lookInput = playerInput.Player.Look.ReadValue<Vector2>();
    8.         Vector2 moveInput = playerInput.Player.Move.ReadValue<Vector2>();
    9.  
    10.         FixedUpdateInputEventArgs inputArgs = new FixedUpdateInputEventArgs();
    11.         inputArgs.lookInput = lookInput;
    12.         inputArgs.moveInput = moveInput;
    13.  
    14.         inputArgs.mouseSensitivity = orbCam.GetMouseSensitivity();
    15.  
    16.         //...some other code
    17.         if(IsHost){
    18.              //code
    19.         }
    20.         else if(IsClient){
    21.             //..some code above this
    22.             SendInputServerRpc(inputArgs);
    23.        }
    24.     }
    The ServerRpc and the FixedUpdate are in the same class. I put a breakpoint in FixedUpdate on the client side to confirm that the mouse input is definitely not (0, 0). I put a breakpoint on the server side in the ServerRpc and I'm seeing the mouse input is (0, 0) whenever I call the ServerRpc. Have I implemented the custom serialization wrong?
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
  3. bajabes2

    bajabes2

    Joined:
    Jul 25, 2018
    Posts:
    3
    brainwipe and luke-unity like this.