Hello, I am new to netcode, I need to make sure that my network varible _numOfConnectedClients on server is synced, or that the next tick has occured, before I call a clientRpc to run on client side. I have assigned a method to OnClientConnectedCallBack action called OnPlayerConnected, see below.
public void StartHost()
{
// ...other stuff here
NetworkManager.Singleton.OnClientConnectedCallback += OnPlayerConnected;
// ... other stuff here
NetworkManager.Singleton.StartHost();
}
The OnPlayerConnected calls a ClientRpc to update the number of connected clients for each client to display on ui
private void OnPlayerConnected(ulong client)
{
Debug.Log("NcGameManager: OnPlayerConnected Callback");
if (IsServer) {
_numOfConnectedClients.Value = (uint)NetworkManager.Singleton.ConnectedClients.Count;
OnClientConnectedClientRpc(default);
}
}
The OnClientConnectedRpc runs on each client and updates the ui by grabbing the value from an uint network variable
[ClientRpc]
public void OnClientApprovedClientRpc(ClientRpcParams clientRpcParams)
{
Debug.Log("NcGameMan: OnClientApprovedClientRpc call");
var connectedClientsNum = _numOfConnectedClients.Value;
intermissionCanvas.MessageTMP.text = $"Preparing {connectedClientsNum}/4 connected
players, please wait for the game to start.";
}
The issue is, network variable is ticked 30x per sec, and hasn’t been synced when clientrpc is called. hence the number of clients connected on Client side is the previous number of clients before this client joined, or the last client that joined. So how can I force a tick, or wait until a tick has occured, before calling the clientrpc in OnPlayerConnected(ulong client)? Thanks.