Search Unity

ClientRPC firing intermittently

Discussion in 'Netcode for GameObjects' started by TaleOf4Gamers, Aug 25, 2021.

  1. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Hey!

    I have an extremely strange issue that I am trying to debug and hopefully someone can notice where I am going wrong.

    Here is some code to help you get the idea but the gist of it is that this code is for syncing the quests when a player joins a game. This way is not the best thats for sure but I would like to make it functional before I start refactoring.

    So this piece of code will occur when the first player has started hosting and they have loaded into the first level.

    Code (CSharp):
    1. using MLAPI;
    2. using MLAPI.Messaging;
    3. using RPG.Dto;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class NetworkStateManager : NetworkBehaviour
    8. {
    9.     private static NetworkStateManager Instance;
    10.  
    11.     private void Awake()
    12.     {
    13.         if (Instance != null && Instance != this)
    14.         {
    15.             Destroy(gameObject);
    16.         }
    17.         else
    18.         {
    19.             Instance = this;
    20.             DontDestroyOnLoad(gameObject);
    21.         }
    22.     }
    23.  
    24.     private void Start()
    25.     {
    26.         NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected;
    27.     }
    28.  
    29.     private void OnClientConnected(ulong clientID)
    30.     {
    31.         OnClientConnectedServerRpc(clientID);
    32.     }
    33.  
    34.     [ServerRpc(RequireOwnership = false)]
    35.     private void OnClientConnectedServerRpc(ulong clientID)
    36.     {
    37.         if (IsHost == false)
    38.         {
    39.             return;
    40.         }
    41.  
    42.         Debug.Log($"Syncing quests to the new client with ID:{clientID}");
    43.  
    44.         List<Quest> quests = QuestManager.Instance.GetStartedQuests();
    45.         List<QuestDto> questsData = new List<QuestDto>();
    46.  
    47.         foreach (Quest quest in quests)
    48.         {
    49.             QuestDto dto = new QuestDto()
    50.             {
    51.                 NpcNetworkID = quest.NpcNetworkID,
    52.                 QuestID = quest.QuestID,
    53.                 GoalDescription = quest.CurrentGoal.Description,
    54.                 GoalAmount = quest.CurrentGoal.AmountCurrent
    55.             };
    56.  
    57.             questsData.Add(dto);
    58.         }
    59.  
    60.         QuestManager.Instance.SyncQuestsClientRpc(questsData.ToArray());
    61.     }
    62. }
    63.  
    In the QuestManager here it will loop through each given NPC until we find the correct quest and then through each goal until we have the correct goal and set the correct amounts.

    Code (CSharp):
    1.     [ClientRpc]
    2.     public void SyncQuestsClientRpc(QuestDto[] quests)
    3.     {
    4.         Debug.Log("Beginning quest sync with host");
    5.         StartedQuests.Clear();
    6.         CompletedQuests.Clear();
    7.  
    8.         foreach (QuestDto quest in quests)
    9.         {
    10.             NetworkObject network = GetNetworkObject(quest.NpcNetworkID);
    11.             Npc npc = network.GetComponent<Npc>();
    12.  
    13.             Quest currentQuest = null;
    14.  
    15.             while (true)
    16.             {
    17.                 npc.StartNextQuestClient();
    18.  
    19.                 currentQuest = npc.GetCurrentQuest();
    20.  
    21.                 if (currentQuest.QuestID.Equals(quest.QuestID) == false)
    22.                 {
    23.                     npc.CompleteQuestClient();
    24.                     CompletedQuests.Add(currentQuest);
    25.                 }
    26.                 else
    27.                 {
    28.                     StartedQuests.Add(currentQuest);
    29.                     break;
    30.                 }
    31.             }
    32.  
    33.             while (true)
    34.             {
    35.                 QuestGoal goal = currentQuest.CurrentGoal;
    36.  
    37.                 if (goal.Description != quest.GoalDescription)
    38.                 {
    39.                     for (int i = 0; i < (goal.AmountRequired - goal.AmountCurrent); i++)
    40.                     {
    41.                         goal.OnEvent(null, goal.RequiredID);
    42.                     }
    43.                 }
    44.                 else
    45.                 {
    46.                     goal.AmountCurrent = quest.GoalAmount;
    47.  
    48.                     break;
    49.                 }
    50.             }
    51.         }
    52.     }
    This is no so much about this code itself but about the formatting of when my network methods are called for example as I attached a debugger to the built game as well as some logs and sometimes the ClientRPC just does not fire.

    Feel free to ask any questions and I will do my best to answer.
     
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Bump! I am still stuck on this issue and it is a bit of a blocker unfortunately. Does anyone have any ideas?