Jaydon
Jaydon5w ago

Random lobby seed

how do I make a room seed that is set when a person joins the lobby and is synced to everyone?
5 Replies
kapa.ai
kapa.ai5w ago
Hi @Jaydon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To create a random room seed that is set when the first person joins a Normcore lobby and is synced to everyone, you should use a custom RealtimeModel to store the seed value. This ensures the seed is synchronized across all clients, including late joiners. Here’s a high-level approach based on the official guides: 1. Create a RealtimeModel for the seed:
Define a model class (e.g., RoomSeedModel) with a private field for the seed, marked with the [RealtimeProperty] attribute.
using Normal.Realtime;
using Normal.Realtime.Serialization;

[RealtimeModel]
public partial class RoomSeedModel {
[RealtimeProperty(1, true)] private int _seed;
}

using Normal.Realtime;
using Normal.Realtime.Serialization;

[RealtimeModel]
public partial class RoomSeedModel {
[RealtimeProperty(1, true)] private int _seed;
}

2. Create a RealtimeComponent to manage the seed:
Inherit from RealtimeComponent<RoomSeedModel>. When the component is initialized, check if the seed is unset (e.g., zero). If so, generate a random seed and set it in the model. This will sync the value to all clients.
using UnityEngine;
using Normal.Realtime;

public class RoomSeed : RealtimeComponent<RoomSeedModel> {
protected override void OnRealtimeModelReplaced(RoomSeedModel previousModel, RoomSeedModel currentModel) {
if (currentModel != null && currentModel.seed == 0 && realtime.isConnectedToRoom) {
// Only the first client to set the seed should do this
currentModel.seed = Random.Range(int.MinValue, int.MaxValue);
}
}

public int Seed => model != null ? model.seed : 0;
}

using UnityEngine;
using Normal.Realtime;

public class RoomSeed : RealtimeComponent<RoomSeedModel> {
protected override void OnRealtimeModelReplaced(RoomSeedModel previousModel, RoomSeedModel currentModel) {
if (currentModel != null && currentModel.seed == 0 && realtime.isConnectedToRoom) {
// Only the first client to set the seed should do this
currentModel.seed = Random.Range(int.MinValue, int.MaxValue);
}
}

public int Seed => model != null ? model.seed : 0;
}

3. Add the component to a GameObject in your scene:
This ensures the seed is generated and synchronized as soon as the room is joined. 4. Access the seed from any client:
All clients can read the seed from the model after joining the room. This approach leverages Normcore’s datastore to ensure the seed is set once and remains consistent for all clients, including those who join later. For more details on synchronizing custom data, see the official guide: Synchronizing Custom Data. If you need a full working example, you can adapt the pattern from the Timers + Synchronized Playback recipe, replacing the time field with your seed. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5w ago
<@891438191020744704> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
maxweisel
maxweisel4w ago
What do you mean synced to everyone? Like a random number that's used to generate a level? The bot's answer here is correct.
Jaydon
JaydonOP4w ago
Yes, sorry. I figured it out myself and I didn't realize that I downvoted. My solution was similar to the bot's so it wasn't wrong.
maxweisel
maxweisel4w ago
all good! glad you figured something out

Did you find this page helpful?