Marlon Stein
Marlon Stein4w ago

isOwnedLocallySelf during Start()

I've made a script for setting and syncing a unique ID for my objects; I am generating the UUID and setting it to the model during the Start() function here:
private void Start()
{
if (realtimeView.isOwnedLocallySelf)
{
GenerateAndSetNewUuid();
}
else
{
model.theStringDidChange += OnUUIDUpdated;
}
}
private void Start()
{
if (realtimeView.isOwnedLocallySelf)
{
GenerateAndSetNewUuid();
}
else
{
model.theStringDidChange += OnUUIDUpdated;
}
}
but according to my logs both clients end up calling GenerateAndSetNewUuid()? Is it possible that both clients think they own the object during the Start method? What would be a workaround to this to make sure only one clients tries to set the Uuid and ensures the other clients try to Update the UUID once the first client sets it?
7 Replies
kapa.ai
kapa.ai4w ago
Hi @Marlon Stein. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, it is possible that both clients think they own the object during the Start method. According to the documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeView), accessing the realtimeView property before Start is unreliable. This means that the isOwnedLocallySelf property might not return the correct value during the Start method. A possible workaround could be to delay the ownership check until after the Start method has completed. You could use a coroutine to achieve this. Here's an example:
private IEnumerator Start()
{
// Wait until the next frame to check ownership.
yield return null;

if (realtimeView.isOwnedLocallySelf)
{
GenerateAndSetNewUuid();
}
else
{
model.theStringDidChange += OnUUIDUpdated;
}
}
private IEnumerator Start()
{
// Wait until the next frame to check ownership.
yield return null;

if (realtimeView.isOwnedLocallySelf)
{
GenerateAndSetNewUuid();
}
else
{
model.theStringDidChange += OnUUIDUpdated;
}
}
This will ensure that the realtimeView property has been properly initialized before you check the isOwnedLocallySelf property. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@1140565356239405076> kapa.ai is still learning and improving, please let us know how it did by reacting below
Marlon Stein
Marlon SteinOP4w ago
Hm okay. I don't think your fix is good, but if the property is infact unreliable that's a start.
xulipa
xulipa4w ago
I normally check if the client is connected to the room before doing anything Normcore related - which usually takes a couple of seconds atleast.
Marlon Stein
Marlon SteinOP4w ago
These are Prefabs that only get instantiated once everyone is definitely connected so it shouldn't be something like that?
xulipa
xulipa4w ago
Ahh okay nvm then
Mechabit
Mechabit4w ago
you can try isFreshModel instead in OnModelReplaced event isFreshModel is only true on the instantiating client and the model data will go out with the instantiate packet