beeeen
beeeen4mo ago

What is the best way to sync object references across clients?

I want to be able to synchronize a reference to a game object accross clients? Currently, the way I'm doing it is by creating a unique identifier string in a custom RealtimeComponent that syncs across clients. Then, when we need to reference that game object, we can reference that string, search for it on all clients, and get the game object reference. However, this feels a little clunky. I have to assign this custom component to each object that I want to reference. Is there a better way? I thought about using the viewUUID in realtimeView but it is null for realtimeViews that were instantiated in a prefab. Any ideas?
3 Replies
kapa.ai
kapa.ai4mo ago
Hi @beeeen. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Your current approach of using a unique identifier string in a custom RealtimeComponent is a valid way to synchronize a reference to a game object across clients. However, as you mentioned, it can be a bit clunky as you have to assign this custom component to each object you want to reference. As for using the viewUUID in RealtimeView, you're correct that it is null for RealtimeViews that were instantiated in a prefab. The viewUUID is used to associate scene objects across clients and sessions, and for prefab objects, it is null. Any changes to it will break persistence (source (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeView)). If you want to find a RealtimeView reference at runtime, Normcore doesn't use UUIDs to synchronize views internally. However, you can introduce this functionality by creating a RealtimeComponent that assigns a UUID in OnRealtimeModelReplaced() when getting a fresh model. This will ensure that the component has a unique UUID and that existing models don’t have their UUIDs reassigned by new clients (source (https://normcore.io/documentation/realtime/common-questions#is-there-a-uuid-that-i-can-use-to-find-a-realtimeview-reference-at-runtime)). Another approach could be to use the Instantiate method provided by Normcore's Realtime class. This method instantiates a realtime prefab in the scene on all clients. The fully instantiated game object, along with all RealtimeViews and RealtimeComponents and their models, will be initialized and ready for use immediately. The prefab must contain a RealtimeView on the root and it must live within a Resources folder or Unity will not be able to locate it on other clients (source (https://normcore.io/documentation/reference/classes/Normal.Realtime.Realtime)). Remember, any prefab can be used with Normcore as long as it has a RealtimeView on the root of the game object. Once you've created a realtime prefab, you can instantiate it at runtime using Realtime.Instantiate(). This will create a fresh instance of the prefab on all clients and connect all RealtimeComponents together so that any state they synchronize will automatically be replicated to all clients (source (https://normcore.io/documentation/realtime#prefabs)). In conclusion, while your current approach is valid, you might find it more efficient to use the Instantiate method or to assign a UUID in OnRealtimeModelReplaced(). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@690217885540417680> kapa.ai is still learning and improving, please let us know how it did by reacting below
Camobiwon
Camobiwon4mo ago
<#1215145130797830144> I think something like this is coming officially down the line, as a standalone component AFAIK