cryptomax
cryptomax4mo ago

Should a realtime custom component that is changing every frame be handled any differently?

Or should you still just use a didchange event even if it is changing every frame all the time?
Solution:
did change is probably better then
Jump to solution
5 Replies
kapa.ai
kapa.ai4mo 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: ---------------------- In NormalVR, the RealtimeComponent subclass is responsible for keeping a specific part of your scene in sync. When the game object changes, the component updates the model, and when the model changes, the component updates the game object to match. This is done through a didChange event that fires whenever the model changes either locally or remotely. This event is used to update the scene to match the model. However, if your custom component is changing every frame, you might want to consider the performance implications. The documentation suggests that continuously calling an update method from Update() to make sure the renderer matches the model every frame might not be the best approach. This is because it's susceptible to the project's Script Execution Order which can cause updates to be delayed by a frame. Instead, registering for an event from the model that fires whenever it changes locally or by a remote client is recommended. This event can then be used to call your update method. Here's an example of how you can register for a change event:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Normal.Realtime;

public class ColorSync : RealtimeComponent<ColorSyncModel> {
private MeshRenderer _meshRenderer;

protected override void OnRealtimeModelReplaced(ColorSyncModel previousModel, ColorSyncModel currentModel) {
if (previousModel != null) {
// Unregister from events
previousModel.colorDidChange -= ColorDidChange;
}

if (currentModel != null) {
// Register for events so we'll know if the color changes later
currentModel.colorDidChange += ColorDidChange;
}
}

private void ColorDidChange(ColorSyncModel model, Color value) {
// Update the mesh renderer
UpdateMeshRendererColor();
}

private void UpdateMeshRendererColor() {
// Get the color from the model and set it on the mesh renderer.
_meshRenderer.material.color = model.color;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Normal.Realtime;

public class ColorSync : RealtimeComponent<ColorSyncModel> {
private MeshRenderer _meshRenderer;

protected override void OnRealtimeModelReplaced(ColorSyncModel previousModel, ColorSyncModel currentModel) {
if (previousModel != null) {
// Unregister from events
previousModel.colorDidChange -= ColorDidChange;
}

if (currentModel != null) {
// Register for events so we'll know if the color changes later
currentModel.colorDidChange += ColorDidChange;
}
}

private void ColorDidChange(ColorSyncModel model, Color value) {
// Update the mesh renderer
UpdateMeshRendererColor();
}

private void UpdateMeshRendererColor() {
// Get the color from the model and set it on the mesh renderer.
_meshRenderer.material.color = model.color;
}
}
In this example, the ColorDidChange method is called whenever the model's color property changes, updating the mesh renderer accordingly. So, even if your component is changing every frame, using a didChange event is still a recommended approach. However, you should always consider the performance implications and test your application thoroughly to ensure it performs as expected. Sources: - RealtimeComponent (https://normcore.io/documentation/realtime/realtimecomponent) - Creating a RealtimeComponent subclass (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.ai4mo ago
<@401986039964631060> kapa.ai is still learning and improving, please let us know how it did by reacting below
Mechabit
Mechabit4mo ago
by default I think normcore sends 20 times per second so remote clients probably wont get new data every single frame but really it's up to you which you prefer for local client changes that's really up to you
cryptomax
cryptomax4mo ago
hmmm so 20 didchange events per second is probably actually better off than an Update fetch and set yeah i'm talking about the synced changes
Solution
Mechabit
Mechabit4mo ago
did change is probably better then