Andrei Vaduva
Andrei Vaduva6mo ago

RealtimeCallback(RealtimeModelEvent.OnDidReadProperties)

It seems that the RealtimeCallback always fires even if the RealtimeSet didn't change. This is the code i have:
[RealtimeModel]
public partial class MatchmakingModel
{
[RealtimeProperty(1, true)]
private RealtimeSet<MatchmakingData> _rooms;

public delegate void ModelUpdatedHandler();
public event ModelUpdatedHandler OnModelUpdatedHandler;
[RealtimeCallback(RealtimeModelEvent.OnDidReadProperties)]
public void OnModelUpdatedEvent(PropertyChangeSet changes)
{
OnModelUpdatedHandler?.Invoke();
}
}
[RealtimeModel]
public partial class MatchmakingModel
{
[RealtimeProperty(1, true)]
private RealtimeSet<MatchmakingData> _rooms;

public delegate void ModelUpdatedHandler();
public event ModelUpdatedHandler OnModelUpdatedHandler;
[RealtimeCallback(RealtimeModelEvent.OnDidReadProperties)]
public void OnModelUpdatedEvent(PropertyChangeSet changes)
{
OnModelUpdatedHandler?.Invoke();
}
}
8 Replies
kapa.ai
kapa.ai6mo ago
Hi @Andrei Vaduva. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The RealtimeCallback attribute with RealtimeModelEvent.OnDidReadProperties is designed to fire after a model reads properties from a remote update. This means it will trigger every time there is a deserialization pass, regardless of whether the RealtimeSet has changed or not. If you want to trigger an event only when the RealtimeSet changes, you might want to consider using the modelAdded or modelRemoved events provided by the RealtimeSet class. These events fire when a model is added or removed locally or remotely. Here is an example of how you might use these events:
public partial class MatchmakingModel
{
[RealtimeProperty(1, true)]
private RealtimeSet<MatchmakingData> _rooms;

public MatchmakingModel()
{
_rooms.modelAdded += OnModelAdded;
_rooms.modelRemoved += OnModelRemoved;
}

public delegate void ModelUpdatedHandler();
public event ModelUpdatedHandler OnModelUpdatedHandler;

private void OnModelAdded(MatchmakingData data)
{
OnModelUpdatedHandler?.Invoke();
}

private void OnModelRemoved(MatchmakingData data)
{
OnModelUpdatedHandler?.Invoke();
}
}
public partial class MatchmakingModel
{
[RealtimeProperty(1, true)]
private RealtimeSet<MatchmakingData> _rooms;

public MatchmakingModel()
{
_rooms.modelAdded += OnModelAdded;
_rooms.modelRemoved += OnModelRemoved;
}

public delegate void ModelUpdatedHandler();
public event ModelUpdatedHandler OnModelUpdatedHandler;

private void OnModelAdded(MatchmakingData data)
{
OnModelUpdatedHandler?.Invoke();
}

private void OnModelRemoved(MatchmakingData data)
{
OnModelUpdatedHandler?.Invoke();
}
}
In this example, the OnModelUpdatedHandler event will only fire when a model is added or removed from the _rooms RealtimeSet. Please note that this code is just an example and might need to be adjusted to fit your specific use case. For more information, you can refer to the RealtimeSet<TValue> documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.Serialization.RealtimeSet%601) and the RealtimeModelEvent documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeModelEvent). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6mo ago
<@1217086475661738045> kapa.ai is still learning and improving, please let us know how it did by reacting below
Mechabit
Mechabit6mo ago
model added event doesn't fire if the contents of the set changes a workaround is to add an int property next to the set which you can increment when there's a change
Andrei Vaduva
Andrei Vaduva6mo ago
that's what i would expect. I'm trying to build a simple matchmaking system but i'm having trouble matching 2 users if they join the matchmaking room really quickly, in that case, they both create a room instead of one creating a room and one joining i think i'll fix it with a random delay between 2 values and that's it 😄
Mechabit
Mechabit6mo ago
not much you can do about 2 players creating a match at the same time at least the 3rd player will have a choice of matches lol or you can create a waiting for match queue and only create a match when there's 2 people in the queue
Andrei Vaduva
Andrei Vaduva6mo ago
hmm, that would work
Mechabit
Mechabit6mo ago
with some time-out or start now button
Andrei Vaduva
Andrei Vaduva6mo ago
and the lowest timestamped client will create the room and the other can join