Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Noob Question: Sharing NetworkPlayer instances

Discussion in 'Multiplayer' started by JLavender474, Dec 2, 2023.

  1. JLavender474

    JLavender474

    Joined:
    Aug 26, 2021
    Posts:
    3
    Hey everyone, noob here working on a Unity project for a school final project. I'm developing a multiplayer VR game using Netcode. The issue I'm having is that when a player is added to the dictionary in the NetworkGameManager class, then the debug log states that the size of the dictionary is always one no matter how many players join or leave the game lobby. Any help would be greatly appreciated!

    NetworkGameManager class:
    Code (CSharp):
    1. public class NetworkGameManager : NetworkManager
    2.     {
    3.         private Dictionary<Guid, NetworkPlayer> _playerIdToPlayer = new();
    4.  
    5.         public bool ContainsPlayer(Guid playerId)
    6.         {
    7.             var contains = _playerIdToPlayer.ContainsKey(playerId);
    8.             Debug.Log("[NetworkGameManager] Player with id " + playerId + "is in dictionary: " + contains);
    9.             return contains;
    10.         }
    11.  
    12.         public Guid AddPlayer(NetworkPlayer player)
    13.         {
    14.             var playerId = Guid.NewGuid();
    15.             Debug.Log("[NetworkGameManager] Adding player with id " + playerId + "to dictionary");
    16.            
    17.             _playerIdToPlayer.Add(playerId, player);
    18.             Debug.Log("[NetworkGameManager] Total players after adding player " + _playerIdToPlayer.Count);
    19.             return playerId;
    20.         }
    21.  
    22.         public void RemovePlayer(Guid playerId)
    23.         {
    24.             Debug.Log("[NetworkGameManager] Removing player with id " + playerId + "from dictionary");
    25.             _playerIdToPlayer.Remove(playerId);
    26.             Debug.Log("[NetworkGameManager] Total players after removing player " + _playerIdToPlayer.Count);
    27.         }
    28.  
    29.         public NetworkPlayer GetPlayer(Guid playerId)
    30.         {
    31.             Debug.Log("[NetworkGameManager] Getting player with id " + playerId + "from dictionary");
    32.             return _playerIdToPlayer[playerId];
    33.         }
    34.     }
    NetworkPlayer class:
    Code (CSharp):
    1. public class NetworkPlayer : NetworkBehaviour
    2.     {
    3.         private const int MaxLives = 3;
    4.  
    5.         [SerializeField] public NetworkPlayerHealthScript healthScript;
    6.         [SerializeField] public Transform root;
    7.         [SerializeField] public Transform body;
    8.         [SerializeField] public Transform head;
    9.         [SerializeField] public Transform leftHand;
    10.         [SerializeField] public Transform rightHand;
    11.         [SerializeField] public Renderer[] meshToDisable;
    12.  
    13.         [SerializeField] public int team;
    14.         [SerializeField] public NetworkVariable<int> lives = new(MaxLives);
    15.  
    16.         private Guid _playerId;
    17.  
    18.         public override void OnNetworkSpawn()
    19.         {
    20.             Debug.Log("[NetworkPlayer] On network spawn called");
    21.             base.OnNetworkSpawn();
    22.             if (!IsOwner)
    23.             {
    24.                 Debug.Log("[NetworkPlayer] Not owner so returning early");
    25.                 return;
    26.             }
    27.  
    28.             Debug.Log("[NetworkPlayer] Owner so enabling meshes");
    29.             foreach (var mesh in meshToDisable)
    30.             {
    31.                 mesh.enabled = false;
    32.             }
    33.  
    34.             healthScript.SubscribeToHealthValue(OnHealthChange);
    35.  
    36.             var gameManager = NetworkManager.Singleton.GetComponent<NetworkGameManager>();
    37.             _playerId = gameManager.AddPlayer(this);
    38.         }
    39.  
    40.         public override void OnNetworkDespawn()
    41.         {
    42.             Debug.Log("[NetworkPlayer] On network despawn called");
    43.             base.OnNetworkDespawn();
    44.             if (!IsOwner)
    45.             {
    46.                 Debug.Log("[NetworkPlayer] Not owner so returning early");
    47.                 return;
    48.             }
    49.  
    50.             Debug.Log("[NetworkPlayer] Owner so disabling meshes");
    51.             foreach (var mesh in meshToDisable)
    52.             {
    53.                 mesh.enabled = true;
    54.             }
    55.  
    56.             var gameManager = NetworkManager.Singleton.GetComponent<NetworkGameManager>();
    57.             gameManager.RemovePlayer(_playerId);
    58.         }
    59.  
    60.  
    61.         public Guid GetPlayerId()
    62.         {
    63.             return _playerId;
    64.         }
    65.  
    66.         public float GetHealth()
    67.         {
    68.             return healthScript.GetHealth();
    69.         }
    70.  
    71.         public void SetHealth(int health)
    72.         {
    73.             healthScript.SetHealth(health);
    74.         }
    75.  
    76.         public void TranslateHealth(int health)
    77.         {
    78.             healthScript.TranslateHealth(health);
    79.         }
    80.  
    81.         public bool IsDead()
    82.         {
    83.             return healthScript.IsDead();
    84.         }
    85.  
    86.         private static void OnHealthChange(int previousValue, int newValue)
    87.         {
    88.             Debug.Log("[NetworkPlayer] Health changed from " + previousValue + " to " + newValue);
    89.             VRRigReferences.singleton.healthbarImage.fillAmount = (float)newValue / NetworkPlayerHealthScript.MaxHealth;
    90.             VRRigReferences.singleton.healthbarText.text = "Health: " + newValue;
    91.         }
    92.  
    93.         private void Update()
    94.         {
    95.             if (!IsOwner) return;
    96.  
    97.             var reference = VRRigReferences.singleton;
    98.  
    99.             SetTransform(root, reference.root);
    100.             SetTransform(body, reference.body);
    101.             SetTransform(head, reference.head);
    102.             SetTransform(leftHand, reference.leftHand);
    103.             SetTransform(rightHand, reference.rightHand);
    104.         }
    105.  
    106.         private static void SetTransform(Transform toSet, Transform setFrom)
    107.         {
    108.             toSet.position = setFrom.position;
    109.             toSet.rotation = setFrom.rotation;
    110.             toSet.localScale = setFrom.localScale;
    111.         }
    112.     }
     
  2. Reaktion

    Reaktion

    Joined:
    Nov 8, 2019
    Posts:
    53
    Hi,

    I see a problem inside your method
    AddPlayer()
    of the
    NetworkGameManager
    class. When you try to add a player, you're using the default
    Guid
    as the player id. I believe what you want to do is to retrieve the player id from your
    NetworkPlayer
    by doing :
    Code (CSharp):
    1. var playerId = player.GetPlayerId();
    Of course, don't forget that
    player
    could be null in some case, and that this line could lead to an error in this case.

    Hope this helps !