gubebra
gubebra5mo ago

What can I do if I have dependency between RealtimeProperty?

Lets say I have a model with two properties:
[RealtimeProperty(1, true, true)] private string _text;
[RealtimeProperty(2, true, true)] private float _duration;
[RealtimeProperty(1, true, true)] private string _text;
[RealtimeProperty(2, true, true)] private float _duration;
I want to display the text with a certain duration, but how can I make sure that I have the text synced when duration changes or vice versa?
10 Replies
kapa.ai
kapa.ai5mo ago
Hi @gubebra. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In NormalVR's RealtimeModel, each property is synchronized independently. If you have dependencies between properties, you can use the change event feature to handle this. When you set the last argument of the RealtimeProperty attribute to true, a C# event is added that will fire when a property is changed locally or remotely. This can be a useful signal to update your scene to match the model. In your case, you can add a change event to both _text and _duration. When either of these properties changes, the event will fire, and you can update your display accordingly. Here's an example of how you might do this:
[RealtimeModel]
public partial class MyModel {
[RealtimeProperty(1, true, true)] private string _text;
[RealtimeProperty(2, true, true)] private float _duration;

// This method will be called when either _text or _duration changes
private void OnChange() {
DisplayTextForDuration(_text, _duration);
}
}
[RealtimeModel]
public partial class MyModel {
[RealtimeProperty(1, true, true)] private string _text;
[RealtimeProperty(2, true, true)] private float _duration;

// This method will be called when either _text or _duration changes
private void OnChange() {
DisplayTextForDuration(_text, _duration);
}
}
In this example, OnChange is a method you would define to display the text for the specified duration. Whenever _text or _duration changes, this method will be called, ensuring that your display is always up-to-date. Remember, the change event is a useful tool for handling dependencies between properties, but it's also important to manage your network resources wisely. If you're changing a property very often, you might want to mark it as unreliable to avoid overloading the network with updates. For more information, you can refer to the RealtimeModel documentation (https://normcore.io/documentation/room/realtimemodel) and the guide on Synchronizing Custom Data (https://normcore.io/documentation/realtime/synchronizing-custom-data). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@599054560270483456> kapa.ai is still learning and improving, please let us know how it did by reacting below
gubebra
gubebraOP5mo ago
Yeah @Normcore Assistant but how can I be sure duration updates before text?
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- In NormalVR's RealtimeModel, properties are synchronized independently and there's no built-in way to guarantee the order of updates. However, you can design your model to handle this situation. One approach is to combine your dependent properties into a single property. In your case, you could create a new class that holds both the text and duration, and then use an instance of this class as your RealtimeProperty. Here's an example:
public class TextWithDuration {
public string Text { get; set; }
public float Duration { get; set; }
}

[RealtimeModel]
public partial class MyModel {
[RealtimeProperty(1, true, true)] private TextWithDuration _textWithDuration;

// This method will be called when _textWithDuration changes
private void OnChange() {
DisplayTextForDuration(_textWithDuration.Text, _textWithDuration.Duration);
}
}
public class TextWithDuration {
public string Text { get; set; }
public float Duration { get; set; }
}

[RealtimeModel]
public partial class MyModel {
[RealtimeProperty(1, true, true)] private TextWithDuration _textWithDuration;

// This method will be called when _textWithDuration changes
private void OnChange() {
DisplayTextForDuration(_textWithDuration.Text, _textWithDuration.Duration);
}
}
In this example, _textWithDuration is a single property that holds both the text and the duration. When you update this property, both the text and duration will be updated together, and the OnChange method will be called to display the text for the specified duration. Remember, this approach will increase the amount of data that needs to be synchronized, so it's important to manage your network resources wisely. If you're changing this property very often, you might want to mark it as unreliable to avoid overloading the network with updates. For more information, you can refer to the RealtimeModel documentation (https://normcore.io/documentation/room/realtimemodel) and the guide on Synchronizing Custom Data (https://normcore.io/documentation/realtime/synchronizing-custom-data). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@599054560270483456> kapa.ai is still learning and improving, please let us know how it did by reacting below
Mechabit
Mechabit5mo ago
if you update both at the same time they'll go out in the same packet the update events fire one at a time and not necessaily in the same order so just wait till the end of frame then then get both values
gubebra
gubebraOP5mo ago
Does that happen even if the types are different? A string can be very long and heavy depending on the case
Mechabit
Mechabit5mo ago
if it's too big for one packet they'll split and rejoin like it was one packet
gubebra
gubebraOP5mo ago
Ok so I should be good with waiting next frame regardless?
Mechabit
Mechabit5mo ago
yeah