Niv
Niv4mo ago

Is there a callback when *any* property of a model changes?

In my RealtimeComponent, I have a relatively big model with several properties, is there a way to get a callback when any property changes instead of creating a didChange callback to every property and subscribing/unsubscribing to it?
8 Replies
kapa.ai
kapa.ai4mo ago
Hi @Niv. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, there is a way to get a callback when any property of a model changes. You can use the RealtimeModel's Change event feature. This is an optional argument that specifies whether you would like a change event added to the model. When this is set to true, a C# event that will fire when a property is changed locally or remotely is added to the model by the model compiler. This is a useful signal to update your scene to match the model. Here is an example of how you can use it in your RealtimeComponent:
protected override void OnRealtimeModelReplaced(YourModel previousModel, YourModel currentModel) {
if (previousModel != null) {
// Unsubscribe from events on the old model.
previousModel.changeEvent -= OnChangeEvent;
}
if (currentModel != null) {
// Subscribe to events on the new model
currentModel.changeEvent += OnChangeEvent;
}
}

// Called whenever any property changes
private void OnChangeEvent() {
// Update your scene here
}
protected override void OnRealtimeModelReplaced(YourModel previousModel, YourModel currentModel) {
if (previousModel != null) {
// Unsubscribe from events on the old model.
previousModel.changeEvent -= OnChangeEvent;
}
if (currentModel != null) {
// Subscribe to events on the new model
currentModel.changeEvent += OnChangeEvent;
}
}

// Called whenever any property changes
private void OnChangeEvent() {
// Update your scene here
}
Please note that you need to compile your model again so that the [RealtimeCallback] functionality is added by the model compiler. For more information, you can refer to the RealtimeModel documentation (https://normcore.io/documentation/room/realtimemodel#change-event) and the Synchronizing Custom Data documentation (https://normcore.io/documentation/realtime/synchronizing-custom-data#change-event). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@269927864265211906> kapa.ai is still learning and improving, please let us know how it did by reacting below
Niv
NivOP4mo ago
not the best answer but did generally point me to the right direction 🤷‍♂️
Mechabit
Mechabit4mo ago
you can use the RealtimeCallbackAttribute but I think there's an update coming for it normally it will fire once all the properties are done updating on a model
maxweisel
maxweisel4mo ago
We’re about to ship an OnDidReadModel RealtimeCallback attribute btw it ships in the next version I believe
Niv
NivOP4mo ago
Thanks guys - so currently I am using: [RealtimeCallback(RealtimeModelEvent.OnDidReadProperties)] As I understand this is called after the properties have been updated from a remote owner. Which means properties have their new values, and this will NOT trigger when doing local model updates - right?
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Based on the information provided, it's difficult to pinpoint the exact issue. However, there are a few things you could check: 1. Compilation Errors: Ensure that your project has no compilation errors. If your project has any errors, any changes you make to the model will not be visible to the model editor. This could be the reason why your changes are not being saved or loaded on scene exit and reload. You can refer to the Common Questions documentation (https://normcore.io/documentation/room/common-questions#i-made-changes-to-my-properties-but-when-i-recompile-the-freshly-compiled-model-isnt-updated-why) for more information. 2. RealtimeComponent Syncing: Make sure that your custom RealtimeComponent is syncing correctly. If you've added your custom component to a game object before it was converted to a RealtimeComponent subclass, it might not sync properly. To verify this, look at the RealtimeView's list of components and make sure you see your custom component in the list. If you don't see it, or a RealtimeView has not been created for you automatically, try removing the component and re-adding it to your game object. This will tell Unity to automatically create a RealtimeView and add your RealtimeComponent subclass to its component list. You can refer to the Synchronizing Custom Data FAQ (https://normcore.io/documentation/realtime/synchronizing-custom-data#my-custom-realtimecomponent-isnt-syncing-and-onrealtimemodelreplaced-doesnt-get-called) for more information. 3. Scene Specific Settings: Check if there are any scene-specific settings or components that might be interfering with the texture sync. Make sure that all the necessary components and scripts are properly attached and configured in the new scene. If none of these suggestions solve your issue, I would recommend reaching out to the NormalVR support team or community for more specific assistance. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@1124402583155511357> kapa.ai is still learning and improving, please let us know how it did by reacting below