Array values accessed simultaneously
Hi, just a quick question. I have a model with a realtimearray, which hold float values. If several players are increasing the values, as in array[0].value += 1 more or less at the same time, is it possible that not all increases will be correctly added?
4 Replies
Hi @Octoyisus. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
When using a
If multiple clients increment the same value in a
Collections ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
RealtimeArray
in Normcore, it's important to understand that it is designed for adding models (such as objects or structs), not for storing and updating primitive values like floats directly. In fact, RealtimeArray
is typically used to hold RealtimeModel
instances, not primitive types like float
or int
—so you would usually have an array of models, each with a float property, rather than an array of floats themselves.
Regarding simultaneous updates: Normcore's collections (including RealtimeArray
) are not transactional and use a "last-client-wins" approach for resolving conflicts when multiple clients make changes at the same time. This means that if several clients try to update the same value (e.g., array[0].value += 1
) simultaneously, only the last update received by the server will be applied, and earlier updates may be overwritten. As a result, some increments could be lost if multiple clients are incrementing the same value at the same time.
If you need to ensure that all increments are counted reliably, you would need to implement a custom mechanism, such as using a transactional collection like StringKeyDictionary
(which is transactional and can reject conflicting changes), or by designing your logic so that each client adds its own entry and you sum the results, rather than all clients modifying the same value directly. This is a common challenge in real-time multiplayer systems and is not unique to Normcore.
For more details on how RealtimeArray
works and its limitations, see the documentation: RealtimeArray<TValue> and Collections.
Summary:If multiple clients increment the same value in a
RealtimeArray
at the same time, some increments may be lost due to the "last-client-wins" conflict resolution. Normcore does not merge simultaneous changes to the same value in non-transactional collections like RealtimeArray
—only the last update will persist.Collections ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@286884711874166784> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
if they’re incrementing different elements it will work, if they’re incrementing the same element it’s possibly they’ll overwrite each other
C# is only going to store the final value, not the fact that you incremented it by one, and so that client is just sending a final value to the server
you can use a list of change events if you want to make sure every edit lands e.g. for bullets hits in a shooter