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 }
// ownership will not be instant because of latency if (rv.isOwnedLocallySelf ) { // Do Stuff }
6 Replies
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:
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:
----------------------<@351655082145611778> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
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
Thanks Max
sure thing! let me know if you have any other questions. happy to help
Appriciate it. For now I put a //TODO probably going to be a bug. I will come back to this thread