How to call OnRealtimeModelReplaced() only once
Me and a friend of mine are trying to find a way to call OnRealtimeModelReplaced() function only once. Is there any reliable way to do that?
3 Replies
Hi @✨Mike the Floof Wizard✨. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The knowledge sources indicate that
- There is no way to prevent Normcore from calling
OnRealtimeModelReplaced()
is called by Normcore whenever the model for a RealtimeComponent
is set or replaced. This can happen multiple times during the lifecycle of a component—for example, when connecting to a room, switching rooms, or when the model is replaced with a new instance from the server. This behavior is by design and is essential for proper synchronization and event management in Normcore.
There is no built-in way to ensure that OnRealtimeModelReplaced()
is called only once, as it is intended to be called whenever the model changes. However, if you want to execute some initialization logic only the first time this method is called, you can implement your own flag within your component to track whether your "one-time" logic has already run:
This approach allows you to run specific code only once, even though OnRealtimeModelReplaced()
itself may be called multiple times by Normcore.
To summarize:- There is no way to prevent Normcore from calling
OnRealtimeModelReplaced()
multiple times.
- You can use a flag to ensure your own logic inside it runs only once.
This pattern is consistent with the guidance in the documentation, which explains that OnRealtimeModelReplaced()
is called whenever the model is set or replaced, and that you should use it to manage event subscriptions and initialization logic as needed for your component's lifecycle (Synchronizing Custom Data – Creating a RealtimeComponent subclass).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
you can use realtime.didConnectToRoom instead, it only fires once every model is set up