MasterOX 水
MasterOX 水8mo ago

custom sync question

what is this for and is it required to have it in your script?
protected override void OnRealtimeModelReplaced(ColorSyncModel previousModel, ColorSyncModel currentModel) {
if (previousModel != null) {
// Unregister from events
previousModel.colorDidChange -= ColorDidChange;
}

if (currentModel != null) {
// If this is a model that has no data set on it, populate it with the current mesh renderer color.
if (currentModel.isFreshModel)
currentModel.color = _meshRenderer.material.color;

// Update the mesh render to match the new model
UpdateMeshRendererColor();

// Register for events so we'll know if the color changes later
currentModel.colorDidChange += ColorDidChange;
}
}
protected override void OnRealtimeModelReplaced(ColorSyncModel previousModel, ColorSyncModel currentModel) {
if (previousModel != null) {
// Unregister from events
previousModel.colorDidChange -= ColorDidChange;
}

if (currentModel != null) {
// If this is a model that has no data set on it, populate it with the current mesh renderer color.
if (currentModel.isFreshModel)
currentModel.color = _meshRenderer.material.color;

// Update the mesh render to match the new model
UpdateMeshRendererColor();

// Register for events so we'll know if the color changes later
currentModel.colorDidChange += ColorDidChange;
}
}
7 Replies
kapa.ai
kapa.ai8mo ago
Hi @MasterOX 水. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The OnRealtimeModelReplaced method is part of the RealtimeComponent class in NormalVR's Normcore. It is called when a RealtimeComponent is first created and starts with no model. Normcore populates it once we have successfully connected to the server (or instantly, if we're already connected) and calls OnRealtimeModelReplaced() to provide us with a copy of it. If this RealtimeComponent was previously mapped to a different model (e.g., when switching rooms), it will provide a reference to the previous model in order to allow your component to unregister from events. In the provided code, the method is used to synchronize the color of a game object with the model. If the model is fresh (i.e., it's a model for an object that just got created), it populates it with the color of the MeshRenderer. It then updates the MeshRenderer to match the color stored on the model. Finally, it registers for the colorDidChange event that calls the ColorDidChange method whenever the model's color property changes. This way, if the color changes later, the method will be notified so it can update the mesh renderer. This method is not necessarily required in every script, but it is crucial when you want to synchronize custom data in your Unity application using custom realtime components in Normcore. If you're not using Normcore or not synchronizing custom data, you might not need this method. Here is the code you asked about:
protected override void OnRealtimeModelReplaced(ColorSyncModel previousModel, ColorSyncModel currentModel) {
if (previousModel != null) {
// Unregister from events
previousModel.colorDidChange -= ColorDidChange;
}

if (currentModel != null) {
// If this is a model that has no data set on it, populate it with the current mesh renderer color.
if (currentModel.isFreshModel)
currentModel.color = _meshRenderer.material.color;

// Update the mesh render to match the new model
UpdateMeshRendererColor();

// Register for events so we'll know if the color changes later
currentModel.colorDidChange += ColorDidChange;
}
}
protected override void OnRealtimeModelReplaced(ColorSyncModel previousModel, ColorSyncModel currentModel) {
if (previousModel != null) {
// Unregister from events
previousModel.colorDidChange -= ColorDidChange;
}

if (currentModel != null) {
// If this is a model that has no data set on it, populate it with the current mesh renderer color.
if (currentModel.isFreshModel)
currentModel.color = _meshRenderer.material.color;

// Update the mesh render to match the new model
UpdateMeshRendererColor();

// Register for events so we'll know if the color changes later
currentModel.colorDidChange += ColorDidChange;
}
}
You can find more information about this in the Normcore documentation (https://normcore.io/documentation/realtime/synchronizing-custom-data#creating-a-realtimecomponent-subclass). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@758040514678489279> kapa.ai is still learning and improving, please let us know how it did by reacting below
Mechabit
Mechabit8mo ago
you only need that if you want change events
MasterOX 水
MasterOX 水OP8mo ago
wdym change events?
Mechabit
Mechabit8mo ago
this bit: currentModel.colorDidChange += ColorDidChange; when someone updates the model this method is called
MasterOX 水
MasterOX 水OP8mo ago
i think i understand its 2 am so ill read it again tomorrow but ill add it either way
Mechabit
Mechabit8mo ago
if it's something that changes every frame like position you can just poll the model, or if it only appears on-demand like looking at a leaderboard you can also just read the data when you need it. Change events are for more time critical things like a synced event or particle effect.