5 Replies
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:
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:
----------------------
<@185325614775926784> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai does this work if I call it immediately upon connecting to a room, or do i have to wait?
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:
----------------------
<@185325614775926784> Kapa.ai is still learning and improving, please let me know how I did by reacting below.