ClockworkPirate
ClockworkPirate2mo ago

DidRead method never called in Model

I can see my FireEvent being called on Client 1, but then the DidRead never gets called on the other clients. As a secondary question, should I expect DidRead to also get called on the Client that called FireEvent? Here's my code:
using Normal.Realtime;

[RealtimeModel]
public partial class BasketModel {
[RealtimeProperty(1, true)] private int _trigger;
[RealtimeProperty(2, true)] private int _senderID;

// Used to fire an event on all clients
public void FireEvent(int senderID) {
this.trigger++;
this.senderID = senderID;
}

// An event that consumers of this model can subscribe to in order to respond to the event
public delegate void EventHandler(int senderID);
public event EventHandler eventDidFire;

// A RealtimeCallback method that fires whenever we read any values from the server
[RealtimeCallback(RealtimeModelEvent.OnDidRead)]
private void DidRead()
{
UnityEngine.Debug.LogError("DidRead called");

if (eventDidFire != null && trigger != 0)
eventDidFire(senderID);
}
}
using Normal.Realtime;

[RealtimeModel]
public partial class BasketModel {
[RealtimeProperty(1, true)] private int _trigger;
[RealtimeProperty(2, true)] private int _senderID;

// Used to fire an event on all clients
public void FireEvent(int senderID) {
this.trigger++;
this.senderID = senderID;
}

// An event that consumers of this model can subscribe to in order to respond to the event
public delegate void EventHandler(int senderID);
public event EventHandler eventDidFire;

// A RealtimeCallback method that fires whenever we read any values from the server
[RealtimeCallback(RealtimeModelEvent.OnDidRead)]
private void DidRead()
{
UnityEngine.Debug.LogError("DidRead called");

if (eventDidFire != null && trigger != 0)
eventDidFire(senderID);
}
}
12 Replies
kapa.ai
kapa.ai2mo ago
Hi @ClockworkPirate. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The DidRead method is a RealtimeCallback that is supposed to fire whenever any values are read from the server. If it's not being called on the other clients, there could be a few reasons for this: 1. Network issues: Ensure that all clients are properly connected to the network and that there are no issues with the network connection. 2. Serialization issues: Normcore serializes all updates once per network frame (~20hz) and groups them into the same packet. If there are issues with serialization, the updates might not be sent or received properly. Make sure that your project has no compilation errors, as this could prevent changes from being visible to the model editor. 3. Subscription issues: Ensure that the other clients are properly subscribed to the DidRead event. If they're not subscribed, they won't receive the event when it's fired. As for your secondary question, the DidRead method should be called on the client that called FireEvent as well. This is because the DidRead method is triggered whenever any values are read from the server, regardless of which client initiated the change. Here's a reference to the RealtimeModelEvent documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeModelEvent) and the RealtimeCallback documentation (https://normcore.io/documentation/reference/classes/RealtimeCallbackAttribute) for more information. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@543448871075053570> kapa.ai is still learning and improving, please let us know how it did by reacting below
ClockworkPirate
ClockworkPirateOP2mo ago
@Normcore Assistant how can I confirm that my own client is subscribed to the DidRead? Even the sender isn't receiving this m#nethod call.
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To confirm that your client is subscribed to the DidRead event, you need to ensure that you have subscribed to the eventDidFire event in your code. This is the event that gets triggered inside the DidRead method. Here's an example of how you might do this:
BasketModel model = /* get your model instance here */;
model.eventDidFire += OnEventDidFire;

void OnEventDidFire(int senderID) {
// This method will be called when the eventDidFire event is triggered
UnityEngine.Debug.Log("Event did fire from sender: " + senderID);
}
BasketModel model = /* get your model instance here */;
model.eventDidFire += OnEventDidFire;

void OnEventDidFire(int senderID) {
// This method will be called when the eventDidFire event is triggered
UnityEngine.Debug.Log("Event did fire from sender: " + senderID);
}
In this example, OnEventDidFire is a method that you define in your code. This method will be called whenever the eventDidFire event is triggered. If OnEventDidFire is not being called, then there might be an issue with how you're triggering the eventDidFire event. In your DidRead method, you're only triggering the event if eventDidFire is not null and trigger is not 0. Make sure that these conditions are being met. If you're still having issues, you might want to add some additional logging to your FireEvent and DidRead methods to help debug the issue. Here's the documentation (https://normcore.io/documentation/guides/recipes/rpc-events#recipe) for more information. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@543448871075053570> kapa.ai is still learning and improving, please let us know how it did by reacting below
ClockworkPirate
ClockworkPirateOP2mo ago
@Normcore Assistant The DidRead isn't being called at all and that's the first thing that should fire.
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- If the DidRead method isn't being called at all, it could be due to a few reasons: 1. Network Connection: Ensure that the client is properly connected to the server. If there's a network issue, the client might not be receiving updates from the server. 2. Model Synchronization: Make sure that the model is properly synchronized across all clients. If the model isn't synchronized, the clients might not be receiving updates. 3. RealtimeModelEvent Subscription: The DidRead method is a RealtimeCallback that's supposed to fire when the OnDidRead event is triggered. Make sure that you're properly subscribing to this event. Here's how you can check if you're properly subscribing to the OnDidRead event:
[RealtimeCallback(RealtimeModelEvent.OnDidRead)]
private void DidRead() {
UnityEngine.Debug.Log("DidRead called");
// Rest of your code...
}
[RealtimeCallback(RealtimeModelEvent.OnDidRead)]
private void DidRead() {
UnityEngine.Debug.Log("DidRead called");
// Rest of your code...
}
In this example, DidRead should be called whenever the OnDidRead event is triggered. If DidRead isn't being called, then there might be an issue with how you're subscribing to the OnDidRead event. If you're still having issues, you might want to check the Normcore documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeModelEvent) for more information on how RealtimeModelEvent works. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@543448871075053570> kapa.ai is still learning and improving, please let us know how it did by reacting below
Mechabit
Mechabit2mo ago
why don't you use the model property did change event?
Mechabit
Mechabit2mo ago
Synchronizing Custom Data | Normcore Documenation
In this guide, I’m going to walk you through how to synchronize custom data in your Unity application using custom realtime components.
ClockworkPirate
ClockworkPirateOP2mo ago
Ah sorted it! I ended up just using a RealtimeDictionary in the end and that seemed to sync properly. Still unsure why the DidRead wasn't working, if anyone does know then I'd love to know the answer. Thanks @Mechabit for your suggestion too, I'll keep that in mind in the future
maxweisel
maxweisel2mo ago
worth noting there was a bug with OnDidRead that was fixed in a recent version of Normcore you may want to use the new OnDidReadModel callback event type