Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

UNet StateUpdate channel not working?

Discussion in 'UNet' started by Iron-Warrior, Oct 23, 2015.

  1. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    Hi,

    I'm writing a simple custom Network transform that has the server sending it's position to the client, where it is cached and then interpolated between the latest position received from the server and previous ones (using Valve's multiplayer paradigm.) I'm running into a problem with RPC calls, however, where it seems they are all buffered and then sent as a group, rather than individually. I wrote up some super simple test code to see what was going on:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5.  
    6. public class NetPrefabTest : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     GameObject playerFab;
    10.  
    11.     NetworkClient client;
    12.  
    13.     ConnectionConfig config;
    14.  
    15.     void Awake()
    16.     {
    17.         config = new ConnectionConfig();
    18.  
    19.         config.AddChannel(QosType.StateUpdate);
    20.     }
    21.  
    22.     void InitializeClient()
    23.     {
    24.         ClientScene.RegisterPrefab(playerFab);
    25.  
    26.         client = new NetworkClient();
    27.         client.Configure(config, 8);
    28.  
    29.         client.Connect(inputHost, 7777);
    30.     }
    31.  
    32.     void InitializeServer()
    33.     {
    34.         NetworkServer.Configure(config, 8);
    35.         NetworkServer.Listen(7777);
    36.  
    37.         NetworkServer.RegisterHandler(MsgType.Connect, OnServerConnect);
    38.     }
    39.  
    40.     void OnServerConnect(NetworkMessage netMsg)
    41.     {
    42.         NetworkServer.SetClientReady(netMsg.conn);
    43.  
    44.         GameObject p = Instantiate(playerFab, Vector3.zero, Quaternion.identity) as GameObject;
    45.  
    46.         p.GetComponent<NetPlayerMover>().playerConnectionId = netMsg.conn.connectionId;
    47.        
    48.         NetworkServer.Spawn(p);
    49.     }
    50.  
    51.     string inputHost = "localhost";
    52.  
    53.     void OnGUI()
    54.     {
    55.         if (!NetworkServer.active)
    56.         {
    57.             if (GUI.Button(new Rect(400, 20, 200, 20), "Init Server"))
    58.             {
    59.                 InitializeServer();
    60.             }
    61.         }
    62.  
    63.         if (client == null || !client.isConnected)
    64.         {
    65.             if (GUI.Button(new Rect(100, 20, 200, 20), "Client Connect"))
    66.             {
    67.                 InitializeClient();
    68.             }
    69.  
    70.             inputHost = GUI.TextField(new Rect(100, 60, 200, 20), inputHost);
    71.         }
    72.     }
    73. }
    74.  
    The instantiated prefab has just this attached.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5.  
    6. public class NetRPCTest : NetworkBehaviour
    7. {
    8.     int i = 0;
    9.  
    10.     void Update()
    11.     {
    12.         if (isServer)
    13.         {
    14.             RpcMessageTest(i);
    15.             i++;
    16.         }
    17.     }
    18.  
    19.     [ClientRpc(channel=0)]
    20.     void RpcMessageTest(int index)
    21.     {
    22.         if (!isServer)
    23.             Debug.Log(string.Format("{0}: {1}", index, Time.time));
    24.     }
    25. }
    26.  
    I get all the messages in order, but I usually get a grouping of about 4-5 identical timestamps. I was under the impression that the StateUpdate QoS was intended to just send a single message, rather than buffering them. Is there a way to achieve this? I can't seem to find a global control for the network send rate, since as far as I can tell the Network->Send Rate value (in the project settings) only applies to the old Networking.

    Thanks for any help,
    Erik
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    So I've been going through a bunch of options, and it seems like they all have the same issue. I tried using the SendUnreliableToAll method, but I get these results:



    Where the sender is sending the message each Update frame and logging the Time.time. The receiver then gets all the messages as a group, so they're either being buffered on one end or the other. I tried using a SyncVar with the send interval set super low (0.0001) on the default unreliable channel, but I still get the same results. Am I just going crazy here? There should be some way to control the send interval of the messages, right?