Normcore and Singletons
Hello,
Working on a new project using Normcore and the current lead wants to use a singleton for a few managers and have that information be globally synced between several devices within the experience. Just checking to see if Normcore plays nicely with this or what the recommended alternative would be.
Thanks,
6 Replies
Hi @wwm0nkey. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Hello,
Normcore does not directly mention the use of singletons in its documentation. However, it does provide a robust system for synchronizing state across multiple clients through its datastore. When you make a change to the datastore, it’s automatically replicated to all connected clients. This could potentially serve a similar purpose to a singleton, as it provides a single source of truth that is accessible to all clients.
For example, if you need to move an object in the world, you change its position in the datastore. The datastore will automatically detect any changes and notify all clients connected to the room so they can update their world to match. When a client joins a room late, the server is able to send them a complete copy of the datastore without requiring any intervention from the other clients. To conserve on bandwidth, once the server has sent the initial datastore snapshot, it will only send delta updates for the remainder of the session (source (https://normcore.io/documentation/architecture/client#datastore)).
If you're looking to synchronize a specific piece of data across all clients, you might consider using Normcore's Realtime API. Any prefab can be used with Normcore as long as it has a RealtimeView on the root of the game object. Once you've created a realtime prefab, you can instantiate it at runtime using
Realtime.Instantiate()
. This will create a fresh instance of the prefab on all clients and connect all RealtimeComponents together so that any state they synchronize will automatically be replicated to all clients (source (https://normcore.io/documentation/realtime#prefabs)).
However, if you're looking to use singletons for managing global state, you might want to consider the implications of this design pattern in a multiplayer context. Singletons can be a useful tool for managing global state in a single-player game, but in a multiplayer game, you need to consider how this state will be synchronized across multiple clients. Normcore provides tools for synchronizing state across clients, but it's up to you to decide how to best use these tools in your game's architecture.
I hope this helps! Let me know if you have any other questions.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@138172170965745665> kapa.ai is still learning and improving, please let us know how it did by reacting below
If you put a RealtimeComponent on a game object in the scene you can treat it as a singleton
everything will synchronize correctly
Appreciate it
sure thing!
got everything working. Thanks again. Essentially using this + Databrain to try and decouple as much as I can in a scene so there are little to 0 dependencies. Pretty fun actually lol