Best practices for requesting ownership of RealtimeView and needing to execute logic based on that

In the below pseudo code I believe there would be a bug due to the asynchronous nature of ownership requests ? I'd like to wait for the answer within a reasonable time frame var rv = other.gameObject.GetComponent<RealtimeView>(); // Request network ownership rv.RequestOwnershipOfSelfAndChildren();
// ownership will not be instant because of latency if (rv.isOwnedLocallySelf ) { // Do Stuff }
6 Replies
kapa.ai
kapa.ai3d ago
Hi @JimJamesJimJim. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- You're correct in identifying a potential issue with the code you've provided. The ownership request in Normcore is indeed asynchronous, and there's a better way to handle this scenario. Let's look at the best practices for requesting ownership and executing logic based on that. According to the Normcore documentation on Ownership, when you call RequestOwnership(), Normcore assumes the call will succeed and allows you to use the object immediately. However, it's still a good practice to wait for the ownership change to be confirmed before executing critical logic. Here's a better approach: 1. Use the ownerIDSelfDidChange event to execute logic after ownership changes. 2. Implement a timeout mechanism to handle cases where ownership request might fail. Here's an example of how you could implement this:
private void RequestOwnershipAndExecuteLogic(RealtimeView rv)
{
// Set up a listener for ownership change
rv.ownerIDSelfDidChange += OnOwnershipChanged;

// Request ownership
rv.RequestOwnershipOfSelfAndChildren();

// Set up a timeout coroutine
StartCoroutine(OwnershipTimeout(rv, 5f)); // 5 second timeout
}

private void OnOwnershipChanged(RealtimeView rv, int previousOwnerID)
{
if (rv.isOwnedLocallySelf)
{
// Ownership confirmed, do stuff
DoStuff();

// Remove the listener
rv.ownerIDSelfDidChange -= OnOwnershipChanged;
}
}

private IEnumerator OwnershipTimeout(RealtimeView rv, float timeout)
{
yield return new WaitForSeconds(timeout);

if (!rv.isOwnedLocallySelf)
{
Debug.LogWarning("Ownership request timed out");
// Handle the timeout case
rv.ownerIDSelfDidChange -= OnOwnershipChanged;
}
}
private void DoStuff()
{
// Your logic here
}
private void RequestOwnershipAndExecuteLogic(RealtimeView rv)
{
// Set up a listener for ownership change
rv.ownerIDSelfDidChange += OnOwnershipChanged;

// Request ownership
rv.RequestOwnershipOfSelfAndChildren();

// Set up a timeout coroutine
StartCoroutine(OwnershipTimeout(rv, 5f)); // 5 second timeout
}

private void OnOwnershipChanged(RealtimeView rv, int previousOwnerID)
{
if (rv.isOwnedLocallySelf)
{
// Ownership confirmed, do stuff
DoStuff();

// Remove the listener
rv.ownerIDSelfDidChange -= OnOwnershipChanged;
}
}

private IEnumerator OwnershipTimeout(RealtimeView rv, float timeout)
{
yield return new WaitForSeconds(timeout);

if (!rv.isOwnedLocallySelf)
{
Debug.LogWarning("Ownership request timed out");
// Handle the timeout case
rv.ownerIDSelfDidChange -= OnOwnershipChanged;
}
}
private void DoStuff()
{
// Your logic here
}
This approach has several advantages: 1. It uses the ownerIDSelfDidChange event, which is fired when ownership changes, ensuring your logic runs at the right time. 2. It implements a timeout mechanism to handle cases where the ownership request might fail. 3. It properly cleans up the event listener to prevent memory leaks. Remember, as mentioned in the Normcore documentation, a RequestOwnership() call is only rejected if the RealtimeView or a parent RealtimeView is owned by a different client. In most cases, your ownership request should succeed quickly. By following these best practices, you can ensure that your logic executes correctly after ownership is confirmed, while also handling potential edge cases where ownership requests might fail or time out. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3d ago
<@351655082145611778> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
maxweisel
maxweisel3d ago
fwiw we process the ownership change instantly. so if you check ownership after you request it, it will show that you own the object. If the server rejects the request, it will be rolled back, and the ownerIDDidChange event will fire
JimJamesJimJim
JimJamesJimJimOP12h ago
Thanks Max
maxweisel
maxweisel12h ago
sure thing! let me know if you have any other questions. happy to help
JimJamesJimJim
JimJamesJimJimOP12h ago
Appriciate it. For now I put a //TODO probably going to be a bug. I will come back to this thread

Did you find this page helpful?