Andrei Vaduva
Andrei Vaduva2mo ago

Detecting players in room

How can i get the number of players connected to a room. Does a callback exists for player joined/left the room? Or a property on the room with the player count in the room?
2 Replies
kapa.ai
kapa.ai2mo ago
Hi @Andrei Vaduva. 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 connected to a room, 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 instantiating an avatar prefab to represent the local player as well as 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 localAvatar and avatars properties. The avatars 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. As for callbacks when players join and leave the room, RealtimeAvatarManager includes two events: avatarCreated and avatarDestroyed. You can use these events to be notified when players join and leave the room. Here's an example of how to use these events:
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!
}

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!
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar destroyed!
}
}
Please note that as of now, there is no built-in feature to get a list of all rooms and how many players are in them. This is something you'll need to manage yourself (source (https://normcore.io/documentation/architecture/common-questions#how-do-i-get-a-list-of-all-rooms-and-how-many-players-are-in-them)). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@1217086475661738045> kapa.ai is still learning and improving, please let us know how it did by reacting below