khoi
khoi•3mo ago

RealtimeDictionary.modelReplaced oldModel is null

I see the signature is: delegate void Normal.Realtime.Serialization.RealtimeDictionary<TValue>.ModelReplaced(Normal.Realtime.Serialization.RealtimeDictionary<TValue> dictionary, uint key, TValue oldModel, TValue newModel, bool remote) However, when I try to access oldModel, it says the value is null. Is this expected?
12 Replies
kapa.ai
kapa.ai•3mo ago
Hi @khoi. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, it is possible and expected for oldModel to be null in the modelReplaced event of RealtimeDictionary. According to the documentation, the modelReplaced event is fired when a model is replaced locally or remotely. A model is considered replaced (as opposed to removed and added) if a new model is added that overwrites an existing model under the same key. However, the documentation does not explicitly state under which circumstances oldModel will be null. In general, if there was no previous model under the given key (for example, if this is the first time a model is being set for that key), oldModel would be null. This can happen if the dictionary is being initialized or if the key did not previously exist. If you are seeing oldModel as null, it likely means that there was no previous value for that key at the time of replacement. This behavior is consistent with how many dictionary-like data structures work in C# and Unity, where replacing a value for a non-existent key is effectively an "add" operation, and thus there is no old value to provide. For more details on the event and its parameters, see the documentation: RealtimeDictionary<TValue> ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai•3mo ago
<@412870643533479936> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Cam
Cam•3mo ago
Joining a room for a first time would always have a null old model, there was nothing present beforehand
khoi
khoiOP•3mo ago
OHHH DUHHH šŸ¤¦ā€ā™€ļø
maxweisel
maxweisel•3mo ago
replaced should always pass you the model being replaced there’s a separate event for added / removed would love a repro if you’re seeing this on dictionary.modelReplaced
Cam
Cam•3mo ago
Wouldn't the first join always have the oldmodel be null though? Since there was nothing to replace it with?
maxweisel
maxweisel•3mo ago
We’re not talking about OnRealtimeModelReplaced, this is the RealtimeDictionary modelReplaced event that only fires when you insert a model under an existing key it shouldn’t fire at all on first join
Cam
Cam•3mo ago
ohhhhh my bad hahah
maxweisel
maxweisel•3mo ago
all good!
khoi
khoiOP•3mo ago
šŸ˜Ž
No description
khoi
khoiOP•3mo ago
i clowned by not reading kyle's code
maxweisel
maxweisel•3mo ago
that’ll do it. glad it was an easy fix

Did you find this page helpful?