Yips
Yips3mo ago

How to sync a reference to a gameobject

What is the easiest way to sync and update a gameobject reference
12 Replies
kapa.ai
kapa.ai3mo ago
Hi @Yips. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To synchronize and update a GameObject reference in NormalVR, you can use the RealtimeComponent subclasses, specifically the RealtimeTransform component. Here are the steps: 1. Create an empty scene in Unity. Add a Realtime component to an empty game object and configure the appKey so it can connect. 2. Create a GameObject in the scene and add a RealtimeTransform component to it. This component can be used to synchronize the transform or rigidbody of a GameObject. 3. You can choose to synchronize the position, rotation, and scale of the game object by toggling the respective settings in the RealtimeTransform component. 4. Export a build, and open it next to the editor. Hit Play in Unity. 5. Click the "Request Ownership" button on the RealtimeTransform inspector (not the RealtimeView inspector). 6. Drag the GameObject around in the scene and watch how it updates on the build automatically. Remember, RealtimeTransform does not track the parent of a RealtimeTransform and cannot synchronize it at runtime. If you need to pick up an object, instead of reparenting, create an empty game object on your player character at the pick-up point. Then use a script that synchronizes the world position of the pick-up point object and the RealtimeTransform object that you would like to pick up. If you need to sync a specific kind of object like a TextMesh Pro object, you should create a field for each property on the object that you’d like to sync. Only primitives (int, float, Vector3, string, etc.) can be serialized. For more detailed information, you can refer to the following documentation: - Synchronizing a GameObject (https://normcore.io/documentation/realtime#synchronizing-a-gameobject) - RealtimeTransform (https://normcore.io/documentation/realtime/realtimetransform) - RealtimeComponent (https://normcore.io/documentation/realtime/realtimecomponent) - Common Questions (https://normcore.io/documentation/realtime/common-questions#can-i-reparent-a-game-object-with-a-realtimeview-or-realtimecomponent-on-it) - Synchronizing Custom Data (https://normcore.io/documentation/realtime/synchronizing-custom-data#generating-model-code-in-unity-20192020) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@179797443599269889> kapa.ai is still learning and improving, please let us know how it did by reacting below
Yips
Yips3mo ago
@Normcore Assistant How can I pass a reference to a gameobject to a client
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books:
maxweisel
maxweisel3mo ago
womp the assistant isn’t going to get this one right I don’t think
kapa.ai
kapa.ai3mo ago
---------------------- In NormalVR, you can pass a reference to a GameObject to a client by using the Realtime API. Here's how you can do it: 1. First, ensure that the GameObject you want to reference has a RealtimeView component attached to it. This component is used to identify a GameObject and its RealtimeComponents across multiple clients. 2. Instantiate the GameObject 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. Here's an example of how you can instantiate a GameObject and pass its reference:
GameObject myObject = Realtime.Instantiate("My Object",
ownedByClient: true);
GameObject myObject = Realtime.Instantiate("My Object",
ownedByClient: true);
This code will instantiate a GameObject named "My Object" and ensure that the RealtimeView on the root of the prefab is owned by the local client when instantiated. Remember, the GameObject you want to instantiate must be a "realtime prefab", which means it's synchronized via the Realtime API using realtime views and realtime components. Also, it must live in a Resources folder to ensure it can be loaded at runtime. For more detailed information, you can refer to the following documentation: - Realtime.Instantiate() + ownedByClient (https://normcore.io/documentation/room/ownership-and-lifetime-flags#realtimeinstantiate--ownedbyclient) - Realtime API > Prefabs (https://normcore.io/documentation/realtime#prefabs) - RealtimeView (https://normcore.io/documentation/realtime/realtimeview) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@179797443599269889> kapa.ai is still learning and improving, please let us know how it did by reacting below
maxweisel
maxweisel3mo ago
there isn’t an easy way to serialize a reference I think your best bet is to create a network ID realtime component and have it register itself in a local static collection of all network ID components then you can serialize the ID and look it up on all clients
Yips
Yips3mo ago
Gotcha I'll give that a shot I saw someone post their code snippet of it was just wondering if there was easier way for a one time thing
maxweisel
maxweisel3mo ago
we have a task to build this feature and ship it in Normcore soon, but it wouldn’t be hard to do yourself
Yips
Yips3mo ago
Thank you!
maxweisel
maxweisel3mo ago
sure thing!