MasterOX 水
MasterOX 水3mo ago

how to handle code server side

how do i run a timer for example on the server and get a signal sent to all the clients when that timer reaches 0
5 Replies
kapa.ai
kapa.ai3mo ago
Hi @MasterOX 水. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In NormalVR, you can use the RealtimeComponent to synchronize data across all clients. However, it's important to note that NormalVR doesn't have a traditional server-client architecture where you can run code on the server. Instead, all clients are peers and they synchronize data with each other. For your specific case of a timer, you can create a RealtimeComponent that holds the start time of the timer. All clients can access this start time and calculate the elapsed time locally. When the timer reaches 0, you can handle this event locally on each client. Here's an example of how you can create a stopwatch using a RealtimeComponent:
using Normal.Realtime;

public class Stopwatch : RealtimeComponent<StopwatchModel> {
public float time {
get {
// Return 0 if we're not connected to the room yet.
if (model == null) return 0.0f;

// Make sure the stopwatch is running
if (model.startTime == 0.0) return 0.0f;

// Calculate how much time has passed
return (float)(realtime.room.time - model.startTime);
}
}

public void StartStopwatch() {
model.startTime = realtime.room.time;
}
}
using Normal.Realtime;

public class Stopwatch : RealtimeComponent<StopwatchModel> {
public float time {
get {
// Return 0 if we're not connected to the room yet.
if (model == null) return 0.0f;

// Make sure the stopwatch is running
if (model.startTime == 0.0) return 0.0f;

// Calculate how much time has passed
return (float)(realtime.room.time - model.startTime);
}
}

public void StartStopwatch() {
model.startTime = realtime.room.time;
}
}
In this example, realtime.room.time is a synchronized clock that matches the room server's clock exactly with no added latency. You can start the stopwatch by calling StartStopwatch(), and you can get the elapsed time by accessing the time property. When the timer reaches 0, you can handle this event locally on each client. For example, you can check if time is 0 in your game loop, and if it is, you can trigger whatever event you want. For more information, you can refer to the Timers + Synchronized Playback (https://normcore.io/documentation/guides/recipes/timers-and-synchronized-playback) guide in the NormalVR documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@758040514678489279> kapa.ai is still learning and improving, please let us know how it did by reacting below
MasterOX 水
MasterOX 水3mo ago
so models are data stored on the server, accesible by all clients? @Normcore Assistant
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, that's correct. In NormalVR, models are used to store and synchronize data across all clients. Each model in the datastore, including child models, can have an owner with which it is associated. By default, a model has an ownerID of -1, which denotes that it has no owner. When a model is unowned, it can be modified by anyone in the room. However, when a model is owned by a client, the server only lets the owning client make changes to it or to any of its child models. The datastore is a collection of RealtimeModel objects. These objects make up all of the synchronized states of your application. The root-level model in the datastore is the roomModel that's passed to Room.Connect(). Any models that are not part of realtime components that you would like to store in the datastore need to be a child of this model. For more information, you can refer to the Datastore (https://normcore.io/documentation/room/datastore) and Ownership (https://normcore.io/documentation/room/ownership-and-lifetime-flags) sections in the NormalVR documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@758040514678489279> kapa.ai is still learning and improving, please let us know how it did by reacting below