How to know when to request ownership ASAP?
We have a script that requests ownership of an object right away. However, this throws errors such as:
and our script is:
You can see that we're attempting to determine if the model is set in the datastore with the if conditions:
rv != null && rv.realtime != null && rv.realtime.connected. This obviously doesn't work.
Is there a more elegant way to do this? I haven't seen any properties on the RealtimeView that would indicate that it's been added to the datastore.11 Replies
Hi @beeeen. 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 that simply checking
- For prefabs: Safe to request ownership in
realtime != null && realtime.connected is not sufficient to guarantee that the model has been added to the datastore and is ready for ownership requests. This is a common issue, especially when working with scene objects or when scripts execute before Normcore has finished initializing the model.
The recommended approach is to wait until the model is present before interacting with ownership. For prefabs instantiated with Realtime.Instantiate(), the model is ready by the time Start() is called. For scene objects, you should wait until the didConnectToRoom event has fired or check for model presence in Start() after the connection is established.
From the documentation:
If you’re dealing with a prefab, all views and components are initialized with their models and they will be ready to work with by the timeA more robust solution: - For prefabs: CallStart()is called on any script on the prefab. If you’re dealing with a RealtimeView that exists in a scene file, you’ll need to wait untilrealtime.connected == true, or thedidConnectToRoomevent has fired. RealtimeView usesStart()internally to register with Realtime, and it’s possible your start method ran beforeRealtimeView.Start()Common Questions – RealtimeView.
RequestOwnership() in Start() or later.
- For scene objects: Subscribe to the didConnectToRoom event and call RequestOwnership() after the room is connected.
Example for scene objects:
This ensures you only request ownership after the model is present in the datastore.
Summary:- For prefabs: Safe to request ownership in
Start().
- For scene objects: Wait for didConnectToRoom or check for model presence after connection.
This approach will prevent the "RequestOwnership called before the model has been added to the datastore" exception and is the recommended pattern in Normcore projects Common Questions – RealtimeView.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@690217885540417680> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
hmm this should work actually. can you create an empty unity project that repros this bug? I’ll get it fixed
The only thing I can think of is that if Start() hasn’t been called on your scene view, it won’t have connected to Realtime and received its model yet. are you additively loading a scene here?
or potentially calling this on an instantiated prefab from Awake()
For more context, this is a prefab view. From what I can gather, our scripts call this immediately upon instantiating the object with Realtime.Instantiate(). I can get why the model is not initialized yet. I'm asking as to know where to implement this fix best.
In addition, the error message recommends using SetOwnership instead, would that work?
Full error message:
By the time Realtime.Instantiate() returns all views and components should have models
Would SetOwnership work inside of Awake()?
no
It's not asynchronous, correct?
Awake() runs before Realtime.Instantiate() can set the model and return
it’s synchronous
Realtime.Instantiate is basically just:
GameObject.Instantiate()
SetModel()
return
So I should wait until Start() has been called - gotcha.
I'll try making that Unity Project. Thank you, Max. You're so speedy with these replies!
no this should work. send me a repro and I can get it fixed or advise on what to do
Waiting for Start() only applies to RealtimeViews that existed in the scene and weren’t instantiated from prefabs