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:Jump to 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.
...15 Replies
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
----------------------
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:
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.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:
----------------------
<@272390393495027714> kapa.ai is still learning and improving, please let us know how it did by reacting below
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
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?
yeah or start up a coroutine and wait for end of frame
you could use the avatar created event as well
I believe RealtimeAvatarManager’s avatarCreated will fire too
yeah
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!
if you have client id 0 you're probably the first
oh right... and that will be available in didconnecttoroom
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
seems there are plenty workarounds! ty
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