kyle
kyle3w ago

Determining whether ownership request succeeded

myObject.GetComponent<RealtimeView>().RequestOwnership();

if (myObject.GetComponent<RealtimeView>().isOwnedLocallySelf) {
// Ownership request succeeded
} else {
// Ownership request failed or was denied
}
myObject.GetComponent<RealtimeView>().RequestOwnership();

if (myObject.GetComponent<RealtimeView>().isOwnedLocallySelf) {
// Ownership request succeeded
} else {
// Ownership request failed or was denied
}
Does this work?
3 Replies
kapa.ai
kapa.ai3w ago
Hi @kyle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Your approach is mostly correct, but with an important caveat: the ownership request via RequestOwnership() is asynchronous. This means that immediately after calling RequestOwnership(), the ownership status (isOwnedLocallySelf) may not have updated yet, because the request needs to be processed by the server and then the result is sent back to the client. From the documentation:
It's worth noting that RequestOwnership() and ClearOwnership() take effect immediately, but if the server blocks the request—say, because preventOwnershipTakeover is true or because the view's parent is owned by another client—then ownership and all changes that required ownership will be rolled back.
...
If you're trying to debug ownership settings at runtime, the RealtimeView inspector includes Request Ownership and Clear Ownership buttons that function like calling RequestOwnership() or ClearOwnership() in code.
Requesting ownership of a RealtimeView However, the actual change in ownership is confirmed via the ownerIDSelfDidChange event, not immediately after the method call. So, to reliably determine if the ownership request succeeded, you should listen for the ownerIDSelfDidChange event on the RealtimeView and check isOwnedLocallySelf in the event handler. Example:
RealtimeView view = myObject.GetComponent<RealtimeView>();
view.ownerIDSelfDidChange += (changedView, newOwnerID) => {
if (changedView.isOwnedLocallySelf) {
// Ownership request succeeded
} else {
// Ownership request failed or was denied
}
};
view.RequestOwnership();
RealtimeView view = myObject.GetComponent<RealtimeView>();
view.ownerIDSelfDidChange += (changedView, newOwnerID) => {
if (changedView.isOwnedLocallySelf) {
// Ownership request succeeded
} else {
// Ownership request failed or was denied
}
};
view.RequestOwnership();
This ensures you are responding to the actual ownership change, not just the request. Summary:
Your code will not reliably work as intended if you check isOwnedLocallySelf immediately after RequestOwnership(). Instead, use the ownerIDSelfDidChange event to detect when ownership has actually changed on the client.
Reference: RealtimeView properties and events ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@401595391549898754> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
maxweisel
maxweisel3w ago
there’s a change event for ownerIDDidChange and we’re shipping a new API soon (a month or so) where you can add a callback to RequestOwnership that will fire when it’s confirmed

Did you find this page helpful?