Best practice for scene changing while connecting to a room
I'm making the title screen for my game rn, and the idea is that the user would enter in a room name in the UI, try to connect, and then change scenes to the game when the connection is successful. However, what is happening is that upon a successful connection, all the Realtime prefabs are spawned in the title screen scene and then promptly destroyed after changing scenes to the actual game scene.
What is the way to fix this so that I can connect to the room in the title screen and the objects aren't spawned until after I change scenes? Ideally some way in the title screen to check if a room exists before connecting would be awesome, as then the logic for actually connecting can be done after the scene change. Is that possible?
14 Replies
Hi @Jordan. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To address your question about best practices for scene changing while connecting to a room in Normcore, I can provide some guidance based on the available knowledge sources.
1. Connecting to a room:
The recommended approach is to use the
Realtime.Connect()
method to connect to a room. You can do this in your title screen script without automatically joining a room on start.
2. Checking if a room exists:
Unfortunately, the provided knowledge sources don't contain information about checking if a room exists before connecting. Normcore doesn't seem to have a built-in method for this purpose.
3. Handling scene changes:
To prevent prefabs from spawning in the title screen and then being destroyed, you should consider the following approach:
- Disable the "Join Room on Start" option on your Realtime component in the title screen scene.
- Connect to the room in the title screen, but don't instantiate any prefabs there.
- Use the didConnectToRoom
event to trigger your scene change.
- In your game scene, instantiate the necessary prefabs after the scene has loaded.
Here's a basic example of how you might structure this in your title screen script:
Then, in your game scene, you can instantiate your prefabs after the scene has loaded:
This approach ensures that your prefabs are only instantiated in the game scene after a successful connection.
For more details on connecting to rooms and instantiating prefabs, you can refer to the following Normcore documentation:
- Realtime Class Reference
- Room > Offline Mode
Remember to handle potential connection failures and provide appropriate feedback to the user in your title screen UI.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@817172607438815262> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant ?
Let me check the knowledge base for you again. One moment! :books:
----------------------
I apologize, but you haven't asked a specific question. As an AI assistant specialized in answering questions about NormalVR and Normcore, I'm here to help you with any inquiries related to those topics. Could you please provide a clear question or topic you'd like information about? I'll do my best to assist you based on the available knowledge sources.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@817172607438815262> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant Is there a way to re-instantiate the prefabs associated with a Realtime room instance via a method call?
Let me check the knowledge base for you again. One moment! :books:
----------------------
To answer your question about re-instantiating prefabs associated with a Realtime room instance via a method call, I'll need to refer to the Normcore documentation.
Unfortunately, the knowledge sources provided don't contain specific information about a method to re-instantiate all prefabs associated with a Realtime room instance. Normcore's Realtime API typically handles instantiation of prefabs automatically when connecting to a room or when other clients instantiate objects.
However, there are a few relevant points we can consider:
1. Instantiation of prefabs: Normcore uses
Realtime.Instantiate()
to instantiate prefabs across all clients. This method ensures that the prefab is created and synchronized for all connected clients Realtime API.
2. Datastore: Normcore maintains a datastore that keeps track of all synchronized objects in the room Reference > Classes > Datastore.
3. Custom prefab loading: Normcore allows for custom prefab loading through the IRealtimePrefabLoadDelegate
interface Reference > Classes > IRealtimePrefabLoadDelegate.
Given this information, if you need to re-instantiate prefabs, you might need to implement a custom solution. This could involve:
1. Keeping track of all instantiated prefabs in your own script.
2. Creating a method that destroys all existing prefabs and then re-instantiates them using Realtime.Instantiate()
.
However, it's important to note that this approach might not be ideal for maintaining synchronization across all clients and could potentially cause issues with Normcore's internal state management.
If you need this functionality for a specific use case, it might be better to reconsider your approach or reach out to Normcore's support for more tailored advice. The provided knowledge sources don't offer a built-in method for re-instantiating all prefabs associated with a Realtime room instance.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@817172607438815262> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant Is there a way to re-instantiate the list of synchronized objects stored in the room's datastore for a particular client?
Let me check the knowledge base for you again. One moment! :books:
----------------------
Based on the available knowledge sources, there isn't a direct method to re-instantiate the list of synchronized objects stored in the room's datastore for a particular client in Normcore. However, I can provide some insights based on how Normcore handles object synchronization and instantiation.
1. Datastore Synchronization:
Normcore uses a datastore to keep all clients synchronized. When a client connects to a room, it receives a complete copy of the datastore, and thereafter only receives delta updates Architecture > Unity Client.
2. Prefab Instantiation:
Normcore uses
Realtime.Instantiate()
to create prefabs that are synchronized across all clients Reference > Classes > Realtime.
3. RealtimeSet for Prefabs:
Realtime uses a RealtimeSet to represent instantiated prefabs. All prefabs are stored in a RealtimeSet of RealtimeViewModels Room > Collections.
Given this information, there isn't a built-in method to re-instantiate the list of synchronized objects for a particular client. The datastore is designed to keep all clients in sync automatically, so re-instantiation for a single client isn't a standard operation.
If you're facing issues with object synchronization or need to refresh the state for a particular client, you might want to consider:
1. Reconnecting the client to the room, which would trigger a full datastore synchronization.
2. Implementing a custom solution using the Realtime API to track and re-create objects as needed.
However, these approaches might have implications for performance and synchronization that you'd need to carefully consider.
For more specific solutions or if you're facing a particular problem, it would be best to reach out to Normcore's support team directly. They might be able to provide more tailored advice based on your specific use case and the version of Normcore you're using.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@817172607438815262> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
when you’re ready to reinstantiate all of the prefabs, take the Room object instance from the first Realtime and set it on the Realtime instance for your new scene. The new scene Realtime will adopt the Room object (and its live connection) and reinstantiate and wire up prefabs to it
without disconnecting or reconnecting
You can wait for normcore lobbies to be released which will let you know which rooms are running or you can use unity lobby or some other lobby system.
Or you can have 2 Realtime instances in your scene, one for sending messages and the other one for spawning objects, you don't connect to the objects room until you change scenes so you wont get prefabs spawned into your lobby.
yeah I'm just going to use Unity's lobby system for now I think, will hopefully replace with Normcore's lobby system whenever that gets released