cryptomax
cryptomax7mo ago

Are there any best practices for syncing a large number of variables in one script?

For instance if we have a large script with many bools floats, integers etc and they all need to be synced and will all be changing at different times.
5 Replies
kapa.ai
kapa.ai7mo ago
Hi @cryptomax. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- When it comes to syncing a large number of variables in one script using NormalVR, you can utilize the RealtimeComponent and RealtimeModel classes. A RealtimeComponent subclass script is responsible for keeping a specific part of your scene in sync. It keeps a game object in sync with its corresponding RealtimeModel instance in the datastore. When the game object changes, the component updates the model, and when the model changes, the component updates the game object to match source (https://normcore.io/documentation/realtime/realtimecomponent). For each variable that you want to sync, you can create a field in your RealtimeModel subclass. Only primitives (int, float, Vector3, string, etc.) can be serialized, so if you need to sync a specific kind of object, you should create a field for each property on the object that you’d like to sync source (https://normcore.io/documentation/room/common-questions#when-i-try-to-sync-a-specific-object-to-the-datastore-i-get-an-error-that-says-it-cannot-be-serialized-whats-the-issue). When it comes to syncing properties that change very often, such as animating a color or moving a transform, you should use an unreliable property. Unreliable updates are not resent if they’re dropped in transit because it’s expected that another update is following shortly after. Reliable properties are good for things that you update once and that should be resent if the packet is dropped in transit source (https://normcore.io/documentation/realtime/synchronizing-custom-data#reliable--unreliable). Remember, it's important to ensure that all state that is changed remains in sync across all clients. Desync can result in bugs that are incredibly hard to reproduce and debug source (https://normcore.io/documentation/guides/recipes/rpc-events#preface). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7mo ago
<@401986039964631060> kapa.ai is still learning and improving, please let us know how it did by reacting below
cryptomax
cryptomaxOP7mo ago
@Normcore Assistant can vector4 be synced?
kapa.ai
kapa.ai7mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, a Vector4 can be synced using NormalVR's RealtimeModel. The RealtimeModel is designed to synchronize data that can be easily serialized and transmitted to other clients. This includes primitive types such as int, float, string, and Vector3. Although Vector4 is not explicitly mentioned in the documentation, it should work similarly to Vector3 as they are both Unity's built-in types and can be serialized source (https://normcore.io/documentation/room/realtimemodel). Here's an example of how you might define a Vector4 in a RealtimeModel:
[RealtimeModel]
public partial class MyModel {
[RealtimeProperty(1, true )] private Vector4 _myVector;
}
[RealtimeModel]
public partial class MyModel {
[RealtimeProperty(1, true )] private Vector4 _myVector;
}
Remember to mark the class as partial and use the RealtimeProperty attribute to specify the order and reliability of each property. The boolean in the RealtimeProperty attribute determines whether the property is reliable (true) or unreliable (false). Reliable properties are resent if they're dropped in transit, while unreliable properties are not, as it's expected that another update is following shortly after. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7mo ago
<@401986039964631060> kapa.ai is still learning and improving, please let us know how it did by reacting below

Did you find this page helpful?