HaRrY
HaRrY2mo ago

How to clear RealtimeSet or RealtimeArray

I run matches on loop. And before I start a new match I have to clear the previous scores etc that are stored in RealtimeArray. Lets say there are 10 matches, each match has its score, now this score used to be stored in RealtimeArray, now I am using RealtimeSet so that I can clear it. So I cannot clear RealtimeArray and the only way I can clear RealtimeSet is by using this while (model.advents.Any()) { model.advents.Remove(model.advents.Last()); } And even though I am doing the above only on 1 client sometimes I feel like its not consistent, I dont know even if I should do this or is there a better way to do this. Or how would you fellow normcorians have done it?
5 Replies
kapa.ai
kapa.ai2mo ago
Hi @Harris Saleem. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the knowledge sources, there isn't a direct method to clear a RealtimeSet or RealtimeArray in one operation. However, you can remove elements from a RealtimeSet one by one, as you're currently doing. Here's the method to remove an item from a RealtimeSet:
bool Remove(TValue value)
bool Remove(TValue value)
This method removes a model from the collection and returns a boolean to indicate whether the model was contained within the collection. However, it's important to note that you might encounter issues if multiple clients attempt to delete the same model from a RealtimeSet at the same time. To prevent this, it's generally recommended to have the client that owns the object destroy it:
if (realtimeView.isOwnedLocally) Realtime.Destroy(gameObject);
if (realtimeView.isOwnedLocally) Realtime.Destroy(gameObject);
As for RealtimeArray, it only supports adding elements and does not support removing items by index. If you need a sorted collection that supports random removal, it's recommended to use either RealtimeDictionary with the key as the item index, or a RealtimeSet of models that can be sorted deterministically on all clients when the contents change. Sources: - RealtimeSet (https://normcore.io/documentation/reference/classes/Normal.Realtime.Serialization.RealtimeSet%601) - RealtimeArray (https://normcore.io/documentation/room/collections#realtimearray) - Common Questions (https://normcore.io/documentation/room/common-questions#im-getting-the-error-server-attempted-to-delete-value-from-set-for-key-that-doesnt-exist-in-storage-this-is-a-bug-how-do-i-fix-this) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@250518782341021697> kapa.ai is still learning and improving, please let us know how it did by reacting below
Mechabit
Mechabit2mo ago
wrap the array in a set and clear the set to make a whole new array
HaRrY
HaRrY2mo ago
There is no clear function on RealtimeSet https://normcore.io/documentation/reference/classes/Normal.Realtime.Serialization.RealtimeSet%601 Do you mean the way I am doing above by iterating over each element and removing them from the set?
Mechabit
Mechabit2mo ago
there's only one entry in the set, which is your array by clear I mean remove that one entry