Voltaia
Voltaia4d ago

How can I get the remote ID that an avatar represents?

In the AvatarCreated function avatar.ownerIDSelf returns -1 and avatar.realtime.clientID returns the ID of the local realtime not the remote. I need the ID of the remote client that the avatar is representing.
6 Replies
kapa.ai
kapa.ai4d ago
Hi @Voltaia. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The RealtimeAvatar component has a property called ownerIDSelf which represents the client ID of the component owner. However, as you mentioned, it seems to return -1 when accessed in the AvatarCreated function. This might be because the ownership hasn't been established yet at the time this function is called. One way to handle this is to listen for the ownerIDSelfDidChange event on the RealtimeAvatar component. This event is dispatched when the ownerIDSelf value changes, which should happen when the avatar's ownership is established. Here's an example of how you might set this up:
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) {
// Listen for the ownerIDSelfDidChange event on the avatar
avatar.ownerIDSelfDidChange += AvatarOwnerIDDidChange;
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Stop listening for the ownerIDSelfDidChange event when the avatar is destroyed
avatar.ownerIDSelfDidChange -= AvatarOwnerIDDidChange;
}

private void AvatarOwnerIDDidChange(RealtimeAvatar avatar, int ownerID) {
// The ownerIDSelf value has changed, so now we should have the correct client ID
Debug.Log("Avatar owner ID: " + ownerID);
}
}
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) {
// Listen for the ownerIDSelfDidChange event on the avatar
avatar.ownerIDSelfDidChange += AvatarOwnerIDDidChange;
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Stop listening for the ownerIDSelfDidChange event when the avatar is destroyed
avatar.ownerIDSelfDidChange -= AvatarOwnerIDDidChange;
}

private void AvatarOwnerIDDidChange(RealtimeAvatar avatar, int ownerID) {
// The ownerIDSelf value has changed, so now we should have the correct client ID
Debug.Log("Avatar owner ID: " + ownerID);
}
}
This way, you'll be notified when the ownerIDSelf value changes, and you should be able to get the correct client ID of the remote avatar. Please note that this code is based on the information provided in the RealtimeAvatarManager (https://normcore.io/documentation/realtime/xr/avatars#realtimeavatarmanager) and RealtimeComponent<TModel> (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeComponent%601) documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@276154194606424064> kapa.ai is still learning and improving, please let us know how it did by reacting below
Voltaia
Voltaia4d ago
@Normcore Assistant but I don't need necessarily what client actually owns the avatar, rather which client the avatar represents. I understand this is the same, but is there any way to get it independently?
kapa.ai
kapa.ai4d ago
Let me check the knowledge base for you. One moment! :books: ---------------------- In NormalVR, the avatar represents the client that owns it. The ownerIDSelf property of the RealtimeAvatar component is used to identify the client that the avatar represents. This is because when an avatar is created, the ownership of the root RealtimeView is set to the local client. Therefore, the ownerIDSelf property of the avatar should correspond to the client ID of the client that the avatar represents. However, if you want to get the client ID independently, you can use the clientID property of the Realtime component. The Realtime component is responsible for managing the connection to the Normcore server and it has a clientID property that represents the client ID of the local client. Here's an example of how you can access it:
using UnityEngine;
using Normal.Realtime;

public class MyAvatarManager : MonoBehaviour {
private RealtimeAvatarManager _manager;
private Realtime _realtime;

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

private void AvatarCreated(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Access the clientID property of the Realtime component
Debug.Log("Local client ID: " + _realtime.clientID);
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar destroyed!
}
}
using UnityEngine;
using Normal.Realtime;

public class MyAvatarManager : MonoBehaviour {
private RealtimeAvatarManager _manager;
private Realtime _realtime;

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

private void AvatarCreated(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Access the clientID property of the Realtime component
Debug.Log("Local client ID: " + _realtime.clientID);
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar destroyed!
}
}
Please note that this will give you the client ID of the local client, not the remote client that the avatar represents. If you need to get the client ID of the remote client that the avatar represents, you should use the ownerIDSelf property of the RealtimeAvatar component as explained in the previous answer. For more information, you can refer to the RealtimeAvatarManager (https://normcore.io/documentation/realtime/xr/avatars#realtimeavatarmanager) and Realtime (https://normcore.io/documentation/reference/classes/Normal.Realtime.IRealtimeComponent) documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@276154194606424064> kapa.ai is still learning and improving, please let us know how it did by reacting below
Voltaia
Voltaia4d ago
Ah I needed to use avatar.realtimeView