Understanding clientID values
I want to get the clientID, so I'm checking
realtime.clientID
within OnRealtimeModelReplaced for my RealtimeComponent. The clientID is -1, does that mean I'm not connected when OnRealtimeModelReplaced is called?6 Replies
Hi @Joshua. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To answer your question, let's examine the behavior of
clientID
and the lifecycle of RealtimeComponents in Normcore.
When OnRealtimeModelReplaced
is called for a RealtimeComponent, it doesn't necessarily mean that you're fully connected to the room yet. According to the RealtimeComponent lifecycle documentation (https://normcore.io/documentation/realtime/realtimecomponent#using-views-and-components-in-a-scene), the following sequence occurs:
1. Awake()
and Start()
run, and the RealtimeView registers with Realtime.
2. Realtime creates an empty model for every view and component, setting the model on each and triggering OnRealtimeModelReplaced()
.
3. When Realtime actually connects to a room, it either inserts the model into the datastore (if it's new) or replaces the model with server data (if it exists), triggering another OnRealtimeModelReplaced()
call.
4. Only after this, Realtime.connected
becomes true
, and Realtime.didConnectToRoom
fires.
Given this sequence, it's possible that when OnRealtimeModelReplaced
is first called, you're not yet fully connected to the room. This could explain why you're seeing a clientID
of -1.
To get a valid clientID
, you have a few options:
1. Check Realtime.connected
before accessing clientID
:
2. Wait for the Realtime.didConnectToRoom
event:
3. If you specifically need this in OnRealtimeModelReplaced
, you might want to implement a coroutine to wait for the connection:
It's worth noting that in offline mode, clientID
will always be 0, as mentioned in the offline mode documentation (https://normcore.io/documentation/room/offline-mode#things-to-know).
By using one of these approaches, you should be able to reliably get the clientID
once the connection is established.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@346699113053552640> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
on model replaced does get called before connecting to a room
this is apparently intended behaviour
yeah i forgot >_<
this is actually an excellent answer from the bot
instead you can listen to the didConnectToRoom event
Yeah I do that elsewhere, I just had duplicate code within ...Replaced
so i'll just remove that duplicate, should be fine