jos_valentin
jos_valentin2w ago

How to know when a player disconnects?

How can clients know when other player disconnects from the room? So I can show feedback about that
11 Replies
kapa.ai
kapa.ai2w ago
Hi @jos_valentin. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To detect when a player disconnects from a room in NormalVR's Normcore, you can use the RealtimeAvatarManager component, which provides events that notify you when avatars (representing players) are created or destroyed. Using RealtimeAvatarManager Events The RealtimeAvatarManager includes an avatarDestroyed event that fires when a player's avatar is removed from the room, which happens when they disconnect. Here's how you can implement this:
using UnityEngine;
using Normal.Realtime;

public class PlayerConnectionManager : 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) {
// Player joined - you can show feedback here
Debug.Log($"Player joined: {avatar.gameObject.name}");
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Player disconnected - show feedback here
Debug.Log($"Player disconnected: {avatar.gameObject.name}");
}
}
using UnityEngine;
using Normal.Realtime;

public class PlayerConnectionManager : 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) {
// Player joined - you can show feedback here
Debug.Log($"Player joined: {avatar.gameObject.name}");
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Player disconnected - show feedback here
Debug.Log($"Player disconnected: {avatar.gameObject.name}");
}
}
This script should be attached to the same GameObject that has the RealtimeAvatarManager component, which is typically on the same GameObject as your Realtime instance. Realtime XR Avatars documentation provides more details about the avatar system and these events. Server Disconnect Behavior It's worth noting that when a client crashes or disconnects improperly, the server will detect this after about five seconds:
The server pings connected clients every five seconds. If their connection is dropped suddenly, the server will disconnect them after five seconds and any models that they own that are tagged destroyWhenOwnerLeaves will be destroyed automatically.
Architecture Common Questions This means there might be a short delay between when a player actually disconnects and when other clients are notified of the disconnection. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@391444583034257420> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
jos_valentin
jos_valentinOP2w ago
I see, since I am not developing a XR app, Is there a way to handle this without the RealtimeAvatarManager?
Mechabit
Mechabit2w ago
you can copy the OnDestroy method in the avatar script
jos_valentin
jos_valentinOP2w ago
You mean the OnDestroy method in RealtimeAvatar.cs? seems to need the _realtimeAvatarManager
No description
Mechabit
Mechabit2w ago
when the remote avatar is destroyed you can fire off an event in OnDestroy using your own manager
jos_valentin
jos_valentinOP2w ago
With "avatar" you mean a custom object view for my player or something related to the RealtimeAvatarManager?
Mechabit
Mechabit2w ago
something you instantiate when a player joins, can be anything
jos_valentin
jos_valentinOP2w ago
Is there no way to handle this without having instantiated something for the player (I was trying to handle that stuff in a manager with a RealtimeDictionary)
Mechabit
Mechabit2w ago
can also be a model if the lifetime flag is destory when player leaves would be easier to attach it to a gameobject with a script on it though
jos_valentin
jos_valentinOP2w ago
Ok I'll check if it is posible to use an object to handle with OnDestroy, thanks for the help 🙂

Did you find this page helpful?