Change ownership when RealtimeView owner disconnects?

I have a GameMode manager that is only controllable locally. It works until the host leaves. How would I go about randomly assigning an owner to a realtime view when it's owner leaves the game?
5 Replies
kapa.ai
kapa.ai4w ago
Hi @Horse Eater Beater. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- When the owner of a RealtimeView disconnects, Normcore does not automatically reassign ownership to another client. By default, if the owner leaves and no one else takes ownership, the RealtimeView becomes unowned (ownerID = -1), and any client can then request ownership of it. There is no built-in mechanism to automatically assign a new owner at the moment the previous owner disconnects. To implement random reassignment of ownership when the owner leaves, you would need to: 1. Detect when the owner leaves: You can listen for the ownerIDSelfDidChange event on the RealtimeView. When the ownerID changes to -1, it means the owner has left or cleared ownership. 2. Randomly select a new owner: Have the remaining clients coordinate (for example, via a simple election or random selection process) to decide who should take ownership. 3. Request ownership: The chosen client should call RequestOwnership() on the RealtimeView to become the new owner. Here’s a conceptual outline:
// Subscribe to the ownerIDSelfDidChange event
myRealtimeView.ownerIDSelfDidChange += (view, newOwnerID) => {
if (newOwnerID == -1) {
// Owner left, decide if this client should take ownership
if (ShouldThisClientTakeOwnership()) {
myRealtimeView.RequestOwnership();
}
}
};
// Subscribe to the ownerIDSelfDidChange event
myRealtimeView.ownerIDSelfDidChange += (view, newOwnerID) => {
if (newOwnerID == -1) {
// Owner left, decide if this client should take ownership
if (ShouldThisClientTakeOwnership()) {
myRealtimeView.RequestOwnership();
}
}
};
You’ll need to implement ShouldThisClientTakeOwnership() to coordinate among clients, possibly using a random number or some deterministic method. Important: If you have set preventOwnershipTakeover to true, then ownership cannot be taken over unless it is first cleared by the previous owner. In your scenario, as long as the view becomes unowned when the owner disconnects, other clients can request ownership as described above. If you want the object to persist after the owner leaves, ensure that destroyWhenOwnerOrLastClientLeaves is set to false when instantiating the object or in the inspector settings, otherwise the object will be destroyed when the owner disconnects Ownership + Lifetime Flags. For more details, see the documentation on Requesting ownership of a RealtimeView and Ownership + Lifetime Flags. If you need the reassignment to be fully automatic and deterministic, you may need to implement additional coordination logic among clients, as Normcore does not provide automatic random reassignment out of the box. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@444173592205459466> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Mechabit
Mechabit3w ago
I use the next lowest client id method You don't need to sync anything and everyone agrees on who has the lowest client id
kyle
kyle3w ago
@Mechabit how do you find the next lowest client id?
Mechabit
Mechabit3w ago
you just loop through the avatars in avatar manager to find the lowest

Did you find this page helpful?