Babain
Babain5mo ago

Realtime Avatar manager avatars count issue.

When i connect to a room that already has another connected player, i check for the realtimeavatarmanager.avatars.count inside DidConnectToRoom but it is 0 even though the connecting player ends up in the same room as the already connected one. What could possibly be the origin of this issue?
Solution:
---------------------- The issue you're experiencing might be due to the timing of when you're checking the avatars count. The DidConnectToRoom event is fired as soon as the connection to the room is established, but it doesn't necessarily mean that all the avatars have been instantiated and registered with the RealtimeAvatarManager at that point. 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 is useful for determining how many players are currently in a multiplayer space. However, the avatars might not be instantiated immediately upon connection. ...
Jump to solution
15 Replies
kapa.ai
kapa.ai5mo ago
Hi @Babain. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Solution
kapa.ai
kapa.ai5mo ago
---------------------- The issue you're experiencing might be due to the timing of when you're checking the avatars count. The DidConnectToRoom event is fired as soon as the connection to the room is established, but it doesn't necessarily mean that all the avatars have been instantiated and registered with the RealtimeAvatarManager at that point. 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 is useful for determining how many players are currently in a multiplayer space. However, the avatars might not be instantiated immediately upon connection. If you want to be notified when players join and leave, RealtimeAvatarManager includes two events: avatarCreated and avatarDestroyed. You can use these events to keep track of the avatars in 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!
}
}
In this example, the AvatarCreated and AvatarDestroyed methods will be called whenever an avatar is created or destroyed, respectively. You can use these methods to keep track of the number of avatars in the room.
kapa.ai
kapa.ai5mo ago
Source: RealtimeAvatarManager (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.ai5mo ago
<@272390393495027714> kapa.ai is still learning and improving, please let us know how it did by reacting below
maxweisel
maxweisel5mo ago
this is correct. avatars register with RealtimeAvatarManager in Start() which hasn’t run by the time DidConnectToRoom has fired the prefabs have been instantiated but Start() runs later in the frame
Babain
Babain5mo ago
i see should i use an async function the as my didconnecttorrom function so i know when it is done? or would that still not be enough for start of avatarmanager to run?
maxweisel
maxweisel5mo ago
yeah or start up a coroutine and wait for end of frame
Mechabit
Mechabit5mo ago
you could use the avatar created event as well
maxweisel
maxweisel5mo ago
I believe RealtimeAvatarManager’s avatarCreated will fire too yeah
Babain
Babain5mo ago
what i wanted to do is check if any players are connected before the local client connects, so as i think of it the simplest way to check might be checking for the sum of connected avatars after all avatars have been created if its just one then there are no previously connected clients will try these and see what works, thanks!
Mechabit
Mechabit5mo ago
if you have client id 0 you're probably the first
Babain
Babain5mo ago
oh right... and that will be available in didconnecttoroom
Mechabit
Mechabit5mo ago
I say probably because someone can leave and id gets reused you can also have a bool model which you flip as soon as you connect which you can check to see if someone flipped it
Babain
Babain5mo ago
seems there are plenty workarounds! ty
Mechabit
Mechabit5mo ago
also you'll notice that it takes longer to spin up a room if it's cold than if someone is already in it a cold room takes a couple seconds where as a live room is almost instant