Recommended way to maintain a dict of Realtime prefabs?
I want every player to be able to check a dict or some other data structure to see all the Realtime prefabs of a certain type in a room (and access metadata about those prefabs, like who owns them). I could use a separate RealtimeDictionary and update it when the RT prefabs are instantiated/destroyed, but I imagine it would easily go out of sync if
OnDestroy
isn't always run on disconnecting clients.7 Replies
Hi @kyle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The recommended way to maintain a synchronized collection of Realtime prefabs in Normcore is to leverage the built-in collections provided by the platform, specifically the RealtimeSet or StringKeyDictionary. These collections are designed to handle real-time synchronization and are robust against issues like missed
Use Normcore's built-in
OnDestroy
calls due to unexpected disconnects.
Why not use a custom RealtimeDictionary?
While you could maintain a separate RealtimeDictionary
and update it manually, this approach is prone to synchronization issues, especially if a client disconnects unexpectedly and OnDestroy
is not called. Normcore's built-in lifetime flags and server-side management are specifically designed to handle these edge cases reliably.
Recommended Approach
#### 1. Use RealtimeSet for Prefab Tracking
Normcore internally uses a RealtimeSet<RealtimeViewModel>
to keep track of all instantiated prefabs in a room. The presence of a model in this set signals that a prefab should exist in the scene. This set is automatically updated when prefabs are instantiated or destroyed using Realtime.Instantiate()
and Realtime.Destroy()
. The server ensures that objects are removed from the set when their owner disconnects, even if the client disconnects unexpectedly, thanks to lifetime flags like destroyWhenOwnerOrLastClientLeaves
Room > Collections, Room > Server Authority + Ownership + Lifetime Flags.
#### 2. Accessing Metadata
Each prefab's RealtimeView
and its associated model contain metadata, including ownership information. You can access the owner via the ownerID
property on the relevant model or component (e.g., RealtimeTransform
) Room > Server Authority + Ownership + Lifetime Flags.
#### 3. Querying Prefabs of a Certain Type
If you need to filter prefabs by type, you can store a type identifier as part of the model's metadata or use prefab names. Iterate through the RealtimeSet
and filter based on this property.
#### 4. Avoid Manual Cleanup
Rely on Normcore's server-side cleanup using lifetime flags rather than manual logic in OnDestroy
. This ensures consistency even in the case of abrupt disconnects Room > Server Authority + Ownership + Lifetime Flags.
---
Summary:Use Normcore's built-in
RealtimeSet
to track all prefabs, and rely on server-enforced lifetime flags for cleanup. Access metadata directly from the models in the set. This approach is robust and avoids the pitfalls of manual synchronization logic.
If you need a key-value structure (e.g., mapping prefab IDs to models), consider using StringKeyDictionary
, which is transactional and ensures consistency across clients Room > Collections, StringKeyDictionary Reference.
If you need more specific code examples or details on accessing these collections, let me know!
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@401595391549898754> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Why do you need to sync this? Just have the prefabs self register with a manager locally the same way avatar manager does it
the main use case there is each player in the room has a designated spot in the scene that they own and can customize. when a new player joins, they need to find an open spot. that's what the separately sync'd dict would be for. if I do this locally.. I guess I would need a way of knowing when all the other local RT prefabs are done registering on initial load?
you can use a string key dictionary which will tell you when an entry is accepted so 2 people can't grab the same spot
right, are you suggesting a RT dict for this then? or are you saying I can do this locally and wait for all pre-existing prefabs to self-register?
I'm not sure you can identify a prefab without giving them all unique ids
not really sure what you're trying to do but I'd make a list of spots and a player can claim a spot by putting their client id in a string key dictionary to tell other players they've claimed it