ReeseR
ReeseR2mo ago

How to get localAvatar clientID on connect

Essentially I need the player's number as soon as possible. Currently we're using a coroutine with an arbitrary delay (3 seconds) to wait for the avatar manager to create the avatar. I also didn't want the code to run every time an avatar was created. Is there another option?
7 Replies
kapa.ai
kapa.ai2mo ago
Hi @ReeseR. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, there is another option. You can use the avatarCreated event provided by the RealtimeAvatarManager to get the clientID as soon as the avatar is created. This event is triggered whenever an avatar is created, and it provides the RealtimeAvatarManager, the RealtimeAvatar, and a boolean indicating if it's the local avatar. Here is an example of how you can use this event:
using UnityEngine;
using Normal.Realtime;

public class MyAvatarManager : MonoBehaviour {
private RealtimeAvatarManager _manager;

private void Awake() {
_manager = GetComponent<RealtimeAvatarManager>();
_manager.avatarCreated += AvatarCreated;
}

private void AvatarCreated(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
if (isLocalAvatar) {
// This is the local avatar, get the clientID
int clientID = avatar.realtimeView.ownerID;
// Do something with the clientID
}
}
}
using UnityEngine;
using Normal.Realtime;

public class MyAvatarManager : MonoBehaviour {
private RealtimeAvatarManager _manager;

private void Awake() {
_manager = GetComponent<RealtimeAvatarManager>();
_manager.avatarCreated += AvatarCreated;
}

private void AvatarCreated(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
if (isLocalAvatar) {
// This is the local avatar, get the clientID
int clientID = avatar.realtimeView.ownerID;
// Do something with the clientID
}
}
}
In this code, we're subscribing to the avatarCreated event in the Awake() method. Then, in the AvatarCreated() method, we're checking if the created avatar is the local avatar. If it is, we're getting the clientID from the realtimeView of the avatar. This way, you don't need to wait for an arbitrary delay, and the code will only run when the local avatar is created. You can find more information about the RealtimeAvatarManager and its events in 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.ai2mo ago
<@806620207447146517> kapa.ai is still learning and improving, please let us know how it did by reacting below
ReeseR
ReeseR2mo ago
@Normcore Assistant Doesn't the RealtimeAvatarManager.avatarCreated code run every time an avatar is created? Or does it run only when the local avatar is created?
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The RealtimeAvatarManager.avatarCreated event is triggered every time an avatar is created, not just when the local avatar is created. However, the event provides a boolean parameter isLocalAvatar that indicates whether the created avatar is the local avatar. Here's how you can use this parameter to run code only when the local avatar is created:
private void AvatarCreated(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
if (isLocalAvatar) {
// This is the local avatar, do something
}
}
private void AvatarCreated(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
if (isLocalAvatar) {
// This is the local avatar, do something
}
}
In this code, we're checking if isLocalAvatar is true. If it is, that means the created avatar is the local avatar, and we run the code inside the if statement. If isLocalAvatar is false, that means the created avatar is not the local avatar, and we don't run the code inside the if statement. So, while the AvatarCreated method is called every time an avatar is created, the code inside the if statement only runs when the local avatar is created. You can find more information about this in 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.ai2mo ago
<@806620207447146517> kapa.ai is still learning and improving, please let us know how it did by reacting below
Mechabit
Mechabit2mo ago
realtime.clientID is your local id