fgamez1020
fgamez10203mo ago

Total players in a room in DidConnectToRoom

How to get the total players connected to a room on the DidConnectToRoom method?
2 Replies
kapa.ai
kapa.ai3mo ago
Hi @fgamez1020. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To get the total number of players connected to a room in the DidConnectToRoom method, you can use the RealtimeAvatarManager component. This component lives in the scene on the same game object as your Realtime instance and is responsible for keeping track of avatars that were instantiated for other players in the same room. The RealtimeAvatarManager keeps track of all avatars in the room via the avatars property. This property is a dictionary of all RealtimeAvatar components stored under the clientID that the avatar belongs to. You can use this to determine how many players are currently in a multiplayer space. Here is an example of how you can use it:
using UnityEngine;
using Normal.Realtime;

public class MyAvatarManager : MonoBehaviour {
private RealtimeAvatarManager _manager;

private void Awake() {
_manager = GetComponent<RealtimeAvatarManager>();
_manager.avatarCreated += AvatarCreated;
_manager.avatarDestroyed += AvatarDestroyed;
}

private void AvatarCreated(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar created!
Debug.Log("Total players in room: " + avatarManager.avatars.Count);
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar destroyed!
}
}
using UnityEngine;
using Normal.Realtime;

public class MyAvatarManager : MonoBehaviour {
private RealtimeAvatarManager _manager;

private void Awake() {
_manager = GetComponent<RealtimeAvatarManager>();
_manager.avatarCreated += AvatarCreated;
_manager.avatarDestroyed += AvatarDestroyed;
}

private void AvatarCreated(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar created!
Debug.Log("Total players in room: " + avatarManager.avatars.Count);
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar destroyed!
}
}
In the AvatarCreated method, we log the total number of players in the room by getting the count of the avatars dictionary. Please note that this will give you the total number of players in the room at the time an avatar is created. If you want to get the total number of players in the room at any given time, you can directly access the avatars property of the RealtimeAvatarManager. For more information, you can refer to the RealtimeAvatarManager documentation (https://normcore.io/documentation/realtime/xr/avatars#realtimeavatarmanager). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@1053407831916822528> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?