Alix Ollivier
Alix Ollivier2mo ago

How do I get the number of players in a room?

What's the right variable?
5 Replies
kapa.ai
kapa.ai2mo ago
Hi @Alix Ollivier. 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 number of players in a room in Normcore, you can use the RealtimeAvatarManager component. This component keeps track of all avatars (players) in the room via its avatars property, which is a dictionary mapping client IDs to RealtimeAvatar components. The number of players is simply the count of this dictionary:
int playerCount = realtimeAvatarManager.avatars.Count;
int playerCount = realtimeAvatarManager.avatars.Count;
Here, realtimeAvatarManager is your instance of RealtimeAvatarManager. This approach is recommended for XR projects using avatars, but the same logic applies for any multiplayer project where each player is represented by an avatar prefab managed by RealtimeAvatarManager. For more details, see the XR Avatars documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@185325614775926784> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Alix Ollivier
Alix OllivierOP2mo ago
@kapa.ai does this work if I call it immediately upon connecting to a room, or do i have to wait?
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- You should wait until the connection to the room is fully established before querying the number of players. The avatars dictionary in RealtimeAvatarManager is only populated after the Realtime instance has connected to the room and avatars have been instantiated for each player. The recommended approach is to listen for the didConnectToRoom event on the Realtime component, or use the avatarCreated event on RealtimeAvatarManager to be notified when avatars (players) are created. Querying the avatars dictionary immediately upon connecting may return zero or incomplete results, as the avatars may not have been instantiated yet source. So, always wait for the connection and avatar instantiation events before checking the player count. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@185325614775926784> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?