fgamez1020
fgamez10202w ago

how can I get the number of players connected to a room?

I create a room and when a new player joins I want to know what number of player that user is so I can assign an avatar according to that player number.
5 Replies
kapa.ai
kapa.ai2w 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: ---------------------- You can keep track of the number of players in a room using 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. This can be useful for determining how many players are currently in a multiplayer space. Here is an example of how you can use the RealtimeAvatarManager to track the number of players:
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("Avatar created. Total avatars: " + avatarManager.avatars.Count);
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar destroyed!
Debug.Log("Avatar destroyed. Total avatars: " + avatarManager.avatars.Count);
}
}
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("Avatar created. Total avatars: " + avatarManager.avatars.Count);
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar destroyed!
Debug.Log("Avatar destroyed. Total avatars: " + avatarManager.avatars.Count);
}
}
In this example, whenever a new avatar is created or destroyed, the total number of avatars is logged. This number represents the number of players in the room. Please note that getting a list of all rooms and how many players are in them is something that is currently not supported directly by Normcore and you'll need to manage it yourself. 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.ai2w ago
<@1053407831916822528> kapa.ai is still learning and improving, please let us know how it did by reacting below
fgamez1020
fgamez10202w ago
how can i change materials on a created avatar
Mechabit
Mechabit7d ago
do this: renderer.material = materials[realtime.clientID];